2010-10-06 171 views
6

我想添加一个按钮到自定义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。这按预期工作。为什么它不像命令一样工作?任何线索如何使这项工作?

回答

2

我不确定这是否正确的方法来做到这一点。这是一个有点难以阅读注释的源代码,所以我写这条回复作为答案...

这里是MyListView的+指令结合的方法构造:

public MyListView() 
{   
    showColumnPickerCommand = new RoutedCommand("ShowColumnPickerCommand", typeof(MyListView)); 

    var binding = new CommandBinding(); 
    binding.Command = showColumnPickerCommand; 
    binding.Executed += ShowColumnPicker; 
    binding.CanExecute += ShowColumnPickerCanExecute; 

    CommandBindings.Add(binding); 
} 

private void ShowColumnPicker(object sender, ExecutedRoutedEventArgs e) 
{ 
    MessageBox.Show("Show column picker");   
} 

private void ShowColumnPickerCanExecute(object sender, CanExecuteRoutedEventArgs e) 
{ 
    e.CanExecute = true; 
} 

的绑定未设置在静态上下文中。这是静态的唯一的东西是的DependencyProperty的命令,命令本身:

public static readonly DependencyProperty ShowColumnPickerCommandProperty = 
    DependencyProperty.Register("ShowColumnPickerCommand", typeof(RoutedCommand), typeof(MyListView)); 

private static RoutedCommand showColumnPickerCommand; 

public static RoutedCommand ShowColumnPickerCommand 
{ 
    get 
    { 
     return showColumnPickerCommand; 
    } 
} 

的命令必须是静态的,以能够从XAML像这样绑定到它:

<Button Command="{x:Static local:MyListView.ShowColumnPickerCommand}" /> 
6

你应该做的包装属性命令静态启动和使用

Command={x:Static local:MyListView.MyCustomCommand} 

一般来说,你只需要一个ICommand财产,如果该命令被设置为不同的值上的每个实例(如按钮)或如果它像ViewModel上的DelegateCommand/RelayCommand。您还应该删除getter中的所有额外代码,而是在内联或静态构造函数中初始化命令,并在控件的实例构造函数中连接CommandBinding。

CommandBindings.Add(new CommandBinding(MyCustomCommand, binding_Executed)); 

* * UPDATE

的的RoutedCommand本身应该声明为静态的。当您的控件的外部使用者传入要执行的命令时,ICommand实例属性非常有用,这不是您想要的。这里也不需要DP,并且你正在使用的那个被错误地声明 - 为了可用,它们需要具有GetValue/SetValue的实例包装属性。

public static RoutedCommand ShowColumnPickerCommand 
{ 
    get; private set; 
} 

static MyListView() 
{   
    ShowColumnPickerCommand = new RoutedCommand("ShowColumnPickerCommand", typeof(MyListView)); 
} 
+0

谢谢很多。这解决了我的情况:)现在我可以在命令执行时打开一个Popup。 – 2010-10-06 12:26:11

+0

我遇到了一个新问题...触发命令的按钮只能在窗口的MyListView的第一个实例中使用(启用)。它与关键字Static有什么关系:Command = {x:Static local:MyListView。MyCustomCommand} – 2010-10-07 10:56:32

+0

当命令的CanExecute为false或命令没有附加Execute处理程序时,带命令的按钮被禁用。确保CanExecute中没有任何奇怪的事情发生,并且CommandBinding正在每个ListView实例上设置,而不是静态的上下文中,这只会影响第一个ListView。 – 2010-10-07 11:39:47