If you have an object who is not in herarchical tree, like converter, you cannot bind something even you have define DependyProperty on it.
first you must prepare you converter for having DependencyProperty by override DependencyObject
|
public class SelectionConverter : DependencyObject, IMultiValueConverter |
then create DependencyProperties
|
#region Window (DP SHORT) public object Window { get { return (object)GetValue(WindowProperty); } set { SetValue(WindowProperty, value); } } public static readonly DependencyProperty WindowProperty = DependencyProperty.Register("Window", typeof(object), typeof(SelectionConverter), new PropertyMetadata(null)); #endregion #region Name (DP SHORT) public object Name { get { return (object)GetValue(NameProperty); } set { SetValue(NameProperty, value); } } public static readonly DependencyProperty NameProperty = DependencyProperty.Register("Name", typeof(object), typeof(SelectionConverter), new PropertyMetadata(null)); #endregion |
and finally set binding in xaml
as the converter is not in herarchical tree i cannot using binding with ElementName= but i can use x:reference like this
Une erreur s'est produite. Veuillez ré-essayer plus tard. |
If you want to share a same object created, even this is create in hierachical tree you can by using x:Shared= »true » like this
|
<local:SelectionConverter x:Key="sc" x:Shared="false" Color="Blue"/> |
it’s a good solution for shared parameters on objects
When you want to sort a collection in MVVM you use
|
ICollectionView view = CollectionViewSource.GetDefaultView(Items); view.SortDescriptions.Clear(); view.SortDescriptions.Add(new SortDescription("DirtyName", ListSortDirection.Descending)); |
but if you want to update sorting in treeview for sample you can use
|
ListCollectionView view = new ListCollectionView(collection); view.IsLiveSorting = true; SortDescription sort = new SortDescription("DirtyName", ListSortDirection.Ascending); view.SortDescriptions.Add(sort); |
and set IsLiveSorting to true
for sample see converter using in TreeView for update sorting hierarchical
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
public class TreeViewSortConverter : IValueConverter { public string SortName { get; set; } = null; public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { System.Collections.IList collection = value as System.Collections.IList; ListCollectionView view = new ListCollectionView(collection); view.IsLiveSorting = true; SortDescription sort = new SortDescription(parameter.ToString(), ListSortDirection.Ascending); view.SortDescriptions.Add(sort); return view; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return null; } } |
and using
|
<TreeViewSortConverter x:Key="tvsc" SortName=""/> ...... <TreeView ItemsSource="{Binding Nodes, Converter={StaticResource tvsc}, ConverterParameter=NameDirty}" ..... <HierarchicalDataTemplate DataType="{x:Type local:Node}" ItemsSource="{Binding Items, Converter={StaticResource tvsc}, ConverterParameter=NameDirty}"> ..... |
Commentaires récents