以下是有什么区别的:
所有这些依赖项属性都在Selector类中定义.我经常混淆SelectedItem与SelectedValue,并SelectedValue用SelectedValuePath.
我想知道它们之间的区别,并且当我们使用它们,尤其是SelectedValue和SelectedValuePath.请用一些简单的例子解释它们的用法.
我有以下XAML,ComboBox其中具有代码隐藏SelectionChanged事件处理程序和ViewModel的另一个Command属性。我将SelectedIndex属性设置为0。现在,当我运行项目时,将调用代码隐藏处理程序,但Command不会执行。我想要的是Command应该SelectedIndex=0在视图第一次加载时执行。
<ComboBox Name="listComboBox" SelectionChanged="listComboBox_SelectionChanged" SelectedIndex="0" SelectedValuePath="Content" Margin="5,0" Height="35" Width="150" VerticalAlignment="Center" HorizontalAlignment="Left">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding ListTypeComboSelectionChangedCmd}" CommandParameter="{Binding ElementName=listComboBox, Path=SelectedValue}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<ComboBoxItem Content="ItemOne" />
<ComboBoxItem Content="ItemTwo" />
<ComboBoxItem Content="ItemThree" />
</ComboBox>
Run Code Online (Sandbox Code Playgroud)
更新资料
代码隐藏事件处理程序:
<ComboBox Name="listComboBox" SelectionChanged="listComboBox_SelectionChanged" SelectedIndex="0" SelectedValuePath="Content" Margin="5,0" Height="35" Width="150" VerticalAlignment="Center" HorizontalAlignment="Left">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding ListTypeComboSelectionChangedCmd}" CommandParameter="{Binding ElementName=listComboBox, Path=SelectedValue}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<ComboBoxItem Content="ItemOne" />
<ComboBoxItem Content="ItemTwo" />
<ComboBoxItem Content="ItemThree" />
</ComboBox>
Run Code Online (Sandbox Code Playgroud)
ICommand对象:
private void …Run Code Online (Sandbox Code Playgroud)