我想添加一个按钮到自定义ListView(MyListView),它触发MyListView中定义的命令(MyCustomCommand)。我通过应用ControlTemplate添加了按钮(和标题文本)。问题是,我没有找到一种方法来触发MyCustomCommand单击按钮时。我最终想要实现的是打开一个Popup或ContextMenu,我可以在ListView中选择哪些列应该可见。WPF:绑定到来自ControlTemplate的命令
这里是我的模板来源:
<Style TargetType="local:MyListView">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:MyListView">
<Border Name="Border" BorderThickness="1" BorderBrush="Black">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30" />
<RowDefinition />
</Grid.RowDefinitions>
<Grid Background="LightSteelBlue">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock Margin="3,3,3,3" Text="{TemplateBinding HeaderTitle}" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Stretch" FontSize="16" />
<Button Margin="3,3,3,3" Grid.Column="1"
VerticalAlignment="Center" HorizontalAlignment="Right" Height="20"
Command="{TemplateBinding MyCustomCommand}">A button</Button>
</Grid>
<ScrollViewer Grid.Row="1" Style="{DynamicResource {x:Static GridView.GridViewScrollViewerStyleKey}}">
<ItemsPresenter />
</ScrollViewer>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
这里是MyListView定义:
public class MyListView : ListView
{
public static readonly DependencyProperty MyCustomCommandProperty =
DependencyProperty.Register("MyCustomCommand", typeof(ICommand), typeof(MyListView));
private static RoutedCommand myCustomCommand;
public ICommand MyCustomCommand
{
get
{
if (myCustomCommand == null)
{
myCustomCommand = new RoutedCommand("MyCustomCommand", typeof(MyListView));
var binding = new CommandBinding();
binding.Command = myCustomCommand;
binding.Executed += binding_Executed;
CommandManager.RegisterClassCommandBinding(typeof(MyListView), binding);
}
return myCustomCommand;
}
}
private static void binding_Executed(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("Command Handled!");
}
public static readonly DependencyProperty HeaderTitleProperty =
DependencyProperty.Register("HeaderTitle", typeof(string), typeof(MyListView));
public string HeaderTitle { get; set; }
}
这里是创建MyListView的简单实例的XAML:
<local:MyListView VerticalAlignment="Top" HeaderTitle="ListView title">
<ListView.View>
<GridView>
<GridViewColumn Width="70" Header="Column 1" />
<GridViewColumn Width="70" Header="Column 2" />
<GridViewColumn Width="70" Header="Column 3" />
</GridView>
</ListView.View>
<ListViewItem>1</ListViewItem>
<ListViewItem>2</ListViewItem>
<ListViewItem>1</ListViewItem>
<ListViewItem>2</ListViewItem>
</local:MyListView>
注意绑定到MyListView中的DependencyProperty的HeaderTitle。这按预期工作。为什么它不像命令一样工作?任何线索如何使这项工作?
谢谢很多。这解决了我的情况:)现在我可以在命令执行时打开一个Popup。 – 2010-10-06 12:26:11
我遇到了一个新问题...触发命令的按钮只能在窗口的MyListView的第一个实例中使用(启用)。它与关键字Static有什么关系:Command = {x:Static local:MyListView。MyCustomCommand} – 2010-10-07 10:56:32
当命令的CanExecute为false或命令没有附加Execute处理程序时,带命令的按钮被禁用。确保CanExecute中没有任何奇怪的事情发生,并且CommandBinding正在每个ListView实例上设置,而不是静态的上下文中,这只会影响第一个ListView。 – 2010-10-07 11:39:47