Guest User

Untitled

a guest
Jun 18th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. <!-- Data template for items -->
  2. <DataTemplate DataType="{x:Type local:Widget}">
  3. <StackPanel Orientation="Horizontal">
  4. <StackPanel.ContextMenu>
  5. <ContextMenu>
  6. <MenuItem Header="UseWidget"
  7. Command="{x:Static l:WidgetListControl.UseWidgetCommand}"
  8. CommandParameter="{Binding}" />
  9. </ContextMenu>
  10. </StackPanel.ContextMenu>
  11. <TextBlock Text="{Binding Path=Name}" />
  12. <TextBlock Text="{Binding Path=Price}" />
  13. </StackPanel>
  14. </DataTemplate>
  15.  
  16. <!-- Binding -->
  17. <UserControl.CommandBindings>
  18. <CommandBinding Command="{x:Static l:WidgetListControl.UseWidgetCommand}"
  19. Executed="OnUseWidgetExecuted"
  20. CanExecute="CanUseWidgetExecute" />
  21. </UserControl.CommandBindings>
  22.  
  23. <!-- ItemsControl doesn't work... -->
  24. <ItemsControl ItemsSource="{Binding Path=Widgets}" />
  25.  
  26. <!-- But change it to ListBox, and it works! -->
  27. <ListBox ItemsSource="{Binding Path=Widgets}" />
  28.  
  29. public sealed class WidgetListViewModel
  30. {
  31. public ObservableCollection<Widget> Widgets { get; private set; }
  32.  
  33. public WidgetViewModel()
  34. {
  35. Widgets = new ObservableCollection<Widget>
  36. {
  37. new Widget { Name = "Flopple", Price = 1.234 },
  38. new Widget { Name = "Fudge", Price = 4.321 }
  39. };
  40. }
  41. }
  42.  
  43. public sealed class Widget
  44. {
  45. public string Name { get; set; }
  46. public double Price { get; set; }
  47. }
  48.  
  49. public partial class WidgetListControl
  50. {
  51. public static readonly ICommand UseWidgetCommand
  52. = new RoutedCommand("UseWidget", typeof(WidgetListWindow));
  53.  
  54. public WidgetListControl()
  55. {
  56. InitializeComponent();
  57. }
  58.  
  59. private void OnUseWidgetExecuted(object s, ExecutedRoutedEventArgs e)
  60. {
  61. var widget = (Widget)e.Parameter;
  62. MessageBox.Show("Widget used: " + widget.Name);
  63. }
  64.  
  65. private void CanUseWidgetExecute(object s, CanExecuteRoutedEventArgs e)
  66. {
  67. e.CanExecute = true;
  68. e.Handled = true;
  69. }
  70. }
  71.  
  72. <Style TargetType="{x:Type ListBoxItem}">
  73. <Setter Property="Background" Value="Transparent"/>
  74. <Setter Property="HorizontalContentAlignment" Value="{Binding Path=HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
  75. <Setter Property="VerticalContentAlignment" Value="{Binding Path=VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
  76. <Setter Property="Padding" Value="2,0,0,0"/>
  77. <Setter Property="Template">
  78. <Setter.Value>
  79. <ControlTemplate TargetType="{x:Type ListBoxItem}">
  80. <Border SnapsToDevicePixels="true" x:Name="Bd" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
  81. <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
  82. </Border>
  83. <ControlTemplate.Triggers>
  84. <Trigger Property="IsEnabled" Value="false">
  85. <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
  86. </Trigger>
  87. </ControlTemplate.Triggers>
  88. </ControlTemplate>
  89. </Setter.Value>
  90. </Setter>
  91. </Style>
Add Comment
Please, Sign In to add comment