Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!-- Data template for items -->
- <DataTemplate DataType="{x:Type local:Widget}">
- <StackPanel Orientation="Horizontal">
- <StackPanel.ContextMenu>
- <ContextMenu>
- <MenuItem Header="UseWidget"
- Command="{x:Static l:WidgetListControl.UseWidgetCommand}"
- CommandParameter="{Binding}" />
- </ContextMenu>
- </StackPanel.ContextMenu>
- <TextBlock Text="{Binding Path=Name}" />
- <TextBlock Text="{Binding Path=Price}" />
- </StackPanel>
- </DataTemplate>
- <!-- Binding -->
- <UserControl.CommandBindings>
- <CommandBinding Command="{x:Static l:WidgetListControl.UseWidgetCommand}"
- Executed="OnUseWidgetExecuted"
- CanExecute="CanUseWidgetExecute" />
- </UserControl.CommandBindings>
- <!-- ItemsControl doesn't work... -->
- <ItemsControl ItemsSource="{Binding Path=Widgets}" />
- <!-- But change it to ListBox, and it works! -->
- <ListBox ItemsSource="{Binding Path=Widgets}" />
- public sealed class WidgetListViewModel
- {
- public ObservableCollection<Widget> Widgets { get; private set; }
- public WidgetViewModel()
- {
- Widgets = new ObservableCollection<Widget>
- {
- new Widget { Name = "Flopple", Price = 1.234 },
- new Widget { Name = "Fudge", Price = 4.321 }
- };
- }
- }
- public sealed class Widget
- {
- public string Name { get; set; }
- public double Price { get; set; }
- }
- public partial class WidgetListControl
- {
- public static readonly ICommand UseWidgetCommand
- = new RoutedCommand("UseWidget", typeof(WidgetListWindow));
- public WidgetListControl()
- {
- InitializeComponent();
- }
- private void OnUseWidgetExecuted(object s, ExecutedRoutedEventArgs e)
- {
- var widget = (Widget)e.Parameter;
- MessageBox.Show("Widget used: " + widget.Name);
- }
- private void CanUseWidgetExecute(object s, CanExecuteRoutedEventArgs e)
- {
- e.CanExecute = true;
- e.Handled = true;
- }
- }
- <Style TargetType="{x:Type ListBoxItem}">
- <Setter Property="Background" Value="Transparent"/>
- <Setter Property="HorizontalContentAlignment" Value="{Binding Path=HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
- <Setter Property="VerticalContentAlignment" Value="{Binding Path=VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
- <Setter Property="Padding" Value="2,0,0,0"/>
- <Setter Property="Template">
- <Setter.Value>
- <ControlTemplate TargetType="{x:Type ListBoxItem}">
- <Border SnapsToDevicePixels="true" x:Name="Bd" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
- <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
- </Border>
- <ControlTemplate.Triggers>
- <Trigger Property="IsEnabled" Value="false">
- <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
- </Trigger>
- </ControlTemplate.Triggers>
- </ControlTemplate>
- </Setter.Value>
- </Setter>
- </Style>
Add Comment
Please, Sign In to add comment