Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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
将命令绑定到WPF中的ComboBoxItem_Wpf_Xaml_Combobox_Binding_Command - Fatal编程技术网

将命令绑定到WPF中的ComboBoxItem

将命令绑定到WPF中的ComboBoxItem,wpf,xaml,combobox,binding,command,Wpf,Xaml,Combobox,Binding,Command,我有一个带有组合框的wpf项目。里面的项目是动态填写的。因此,它被绑定到一个包含标签和命令的模型 如果用户在下拉/组合框中选择一项,则应执行该命令。我用一个包含TextBlock和命令超链接的DataTemplate进行了尝试。但该命令仅在选择标签超链接时执行,而在我单击整个项目时不执行 <ComboBox ItemsSource="{Binding Path=States}" SelectedItem="{Binding CurrentState}" > <Combo

我有一个带有组合框的wpf项目。里面的项目是动态填写的。因此,它被绑定到一个包含标签和命令的模型

如果用户在下拉/组合框中选择一项,则应执行该命令。我用一个包含TextBlock和命令超链接的DataTemplate进行了尝试。但该命令仅在选择标签超链接时执行,而在我单击整个项目时不执行

<ComboBox ItemsSource="{Binding Path=States}" SelectedItem="{Binding CurrentState}" >
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock>
                <Hyperlink Command="{Binding Command}" TextDecorations="None" Foreground="Black">
                    <TextBlock Text="{Binding Path=Label}"/>
                </Hyperlink>
            </TextBlock>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>
现在的问题是,如何将命令绑定到ComboBoxItem?

ComboxItem没有Command属性,但可以从CurrentState属性的setter执行命令:

private State _currentState;
public State CurrentState
{
    get { return _currentState; }
    set
    {
        _currentState = value;
        if (_currentState != null)
        {
            _currentState.Command.Execute(null);
        }
    }
}
每当您在组合框中选择项目时,都将设置此属性。另一个选项是处理视图中的SelectionChanged事件