【问题标题】:Binding styled ListBoxItem with RelayCommand使用 RelayCommand 绑定样式 ListBoxItem
【发布时间】:2012-02-23 22:50:43
【问题描述】:
  • 从下面的 DataTemplate 可以看出,我创建了按钮列表视图。我已经为这个按钮指定了 Command 和 CommandParameter。但是当这些按钮是 CanExecute 时,不会触发 Execute 方法。现在,如果我在用户控件上放置一个按钮并绑定命令,事件就会触发。为什么会这样?
       <ListView ItemContainerStyle="{StaticResource AlphabetsContainerStyle}" 
              ItemsSource="{Binding Alphabets}"/>
            <Button Command="{Binding Path=FilterCommand}" CommandParameter="A"/>   <!-- Works -->


            <!-- Code in the Resource Dictionary File -->

            <DataTemplate x:Key="AlphabetsTemplate">
                    <Border>            
                        <Button Content="{Binding}"  
                                Command="{Binding Path=FilterCommand}"
                                CommandParameter="A"/>                   <!-- Doesn't Work -->
                    </Border>
            </DataTemplate>

            <Style TargetType="{x:Type ListBoxItem}" x:Key="AlphabetsContainerStyle">
                <Setter Property="ContentTemplate" Value="{StaticResource AlphabetsTemplate}"/>
            </Style>

**我已删除其他 setter 属性和资源以保持代码视图清洁。

  • 其次,如何用标签替换按钮并将ICommand直接附加到ListBoxItem?
 <!-- Replacing Button with Label -->
    <DataTemplate x:Key="AlphabetsTemplate">
            <Border>            
                <Label Content="{Binding}"         <!-- Label Doesnt have Command Property -->
            </Border>
    </DataTemplate>

 <!-- How can I  set Command directly to ListBoxItem ?-->
    <Style TargetType="{x:Type ListBoxItem}" x:Key="AlphabetsContainerStyle">
        <Setter Property="ContentTemplate" Value="{StaticResource AlphabetsTemplate}"/>
    </Style>

提前谢谢你。 :)

问候,

【问题讨论】:

    标签: c# wpf xaml mvvm icommand


    【解决方案1】:

    似乎 FilterCommand 正在丢失数据上下文,请在定义 FilterCommand 的位置指定 ElementName(例如,将 X:Name 定义到您的窗口并将其作为 Element 提供)

    <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525" x:Name="A1">
        <Window.Resources>
    
            <DataTemplate x:Key="AlphabetsTemplate">
                <Border>
                    <Button Content="{Binding}"  
                            Command="{Binding Path=FilterCommand, ElementName=A1}"
                            CommandParameter="A"/>               
                </Border>
            </DataTemplate>
    

    【讨论】:

      【解决方案2】:

      对于 Button 的情况,您应该更改样式和 DataTemplate 的顺序-

      <DataTemplate x:Key="AlphabetsTemplate">
              <Border>            
                  <Button Content="{Binding}"  
                          Command="{Binding Path=FilterCommand}"
                          CommandParameter="A"/>                   <!-- Doesn't Work -->
              </Border>
      </DataTemplate>    
      
      <Style TargetType="{x:Type ListBoxItem}" x:Key="AlphabetsContainerStyle">
          <Setter Property="ContentTemplate" Value="{StaticResource AlphabetsTemplate}"/>
      </Style>
      

      对于标签,请说明您的要求。

      【讨论】:

      • 顺序和你说的一样,但是为了更好的理解,我这里按层级排列。我正在更改它以避免误解。
      猜你喜欢
      • 1970-01-01
      • 2010-11-04
      • 2011-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-21
      相关资源
      最近更新 更多