Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用RelayCommand绑定样式化ListBoxItem 从下面的DataTemplate可以看出,我已经创建了按钮的listview。我已经为这个按钮指定了Command和CommandParameter。但是当这些按钮被CanExecute和Execute方法调用时。现在,如果在用户控件上放置一个按钮并绑定该命令,则会触发事件。为什么会发生这种情况_C#_Wpf_Xaml_Mvvm_Icommand - Fatal编程技术网

C# 使用RelayCommand绑定样式化ListBoxItem 从下面的DataTemplate可以看出,我已经创建了按钮的listview。我已经为这个按钮指定了Command和CommandParameter。但是当这些按钮被CanExecute和Execute方法调用时。现在,如果在用户控件上放置一个按钮并绑定该命令,则会触发事件。为什么会发生这种情况

C# 使用RelayCommand绑定样式化ListBoxItem 从下面的DataTemplate可以看出,我已经创建了按钮的listview。我已经为这个按钮指定了Command和CommandParameter。但是当这些按钮被CanExecute和Execute方法调用时。现在,如果在用户控件上放置一个按钮并绑定该命令,则会触发事件。为什么会发生这种情况,c#,wpf,xaml,mvvm,icommand,C#,Wpf,Xaml,Mvvm,Icommand,**我已经删除了其他setter属性和资源来保持代码视图的整洁 其次,如何用标签替换按钮并将ICommand直接附加到ListBoxItem 对于按钮案例,您应该更改样式和数据模板的顺序- <DataTemplate x:Key="AlphabetsTemplate"> <Border> <Button Content="{Binding}" Com

**我已经删除了其他setter属性和资源来保持代码视图的整洁

  • 其次,如何用标签替换按钮并将ICommand直接附加到ListBoxItem



对于按钮案例,您应该更改样式和数据模板的顺序-

<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>


对于标签,请指定您的要求。

如果FilterCommand正在丢失datacontext,请在定义FilterCommand的位置指定ElementName(例如,在窗口中定义X:Name并将其作为元素提供)


顺序与您描述的相同,但为了更好地理解,我在这里按层次顺序介绍。我正在改变它以避免误解。
 <!-- 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>
<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>
<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>