if you want to have the same width size for each items in WrapPanel (in in ListBox for sample)
you can use Grid.IsSharedSizeScope= »True » like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
<Window.Resources> <DataTemplate DataType="{x:Type local:ColorX}"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" SharedSizeGroup="ColumnSize" /> </Grid.ColumnDefinitions> <StackPanel Orientation="Horizontal" Margin="2"> <Rectangle Width="40" Height="25" Fill="{Binding Brush}" Stroke="Black" /> <TextBox Text="{Binding Name}" IsReadOnly="true" Background="Transparent" VerticalAlignment="Center" BorderThickness="0" Margin="4 0 4 0" /> </StackPanel> </Grid> </DataTemplate> </Window.Resources> <Grid> <ListBox ItemsSource="{Binding .}" Margin="10" BorderThickness="1" BorderBrush="RoyalBlue" ScrollViewer.HorizontalScrollBarVisibility="Disabled"> <ListBox.ItemsPanel> <ItemsPanelTemplate> <WrapPanel IsItemsHost="True" Grid.IsSharedSizeScope="True" /> </ItemsPanelTemplate> </ListBox.ItemsPanel> </ListBox> </Grid> |
When you try to connect an MVVM command, it must be better to have a fallbackvalue if binding not release
a null static command return false is the solution for showed that the binding cannot be set (and not allow command)
for sample if i have the command : CloseCommand in Selected in my MVVM
when selected is set to null the binding not released
the fallbackvalue is set in this case
I use a static command
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
public class NullCommand : ICommand { private static readonly Lazy<NullCommand> _instance = new Lazy<NullCommand>(() => new NullCommand()); private NullCommand() { } public event EventHandler CanExecuteChanged; public static ICommand Instance { get { return _instance.Value; } } public void Execute(object parameter) { throw new InvalidOperationException("NullCommand cannot be executed"); } public bool CanExecute(object parameter) { return false; } } |
and i use like this
|
<MenuItem Header="sample" Command="{Binding Selected.CloseCommand, FallbackValue={x:Static NullCommand.Instance}}"/> |
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