我想做的是让我的组合框的第一项作为标题。我试着谷歌,发现我可以使用IsSynchronizedWithCurrentItem="True"但问题是我有不同的控件,我隐藏和显示基于Id。
xaml:比方说,在我的xaml for combobox application
<ComboBox HorizontalAlignment="Left" Margin="10,0,0,81" Width="122" VerticalAlignment="Bottom" IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding Path=Applications}" DisplayMemberPath="Name" SelectedValue="{Binding Path= SelectedApplication,Mode=TwoWay}"
SelectedValuePath="Name" />
这个很好,直到我进入这里的combobox transaction
<ComboBox Margin="0,0,732,81" HorizontalAlignment="Right" Width="124" VerticalAlignment="Bottom" IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding Path=TransactionTypes}" DisplayMemberPath="Name" SelectedValue="{Binding Path= SelectedTransaction,Mode=TwoWay}"
SelectedValuePath="Name" Grid.ColumnSpan="2" />
这里,由于第一个元素是由(IsSynchronizedWithCurrentItem="True")在事务组合框上显示的,我的第三个控件是一个网格,我想显示只有当我做一个选择的项目inside transaction是默认显示的,但是我不想在页面加载时加载网格,只有当我做一个选择时,但是我也想要标题。
嗯,没有必要使用VM,因为我认为这只是一个视图。只需将控件绑定到自身。
<!-- your settings are omitted, but you should keep them -->
<ComboBox Text="{Binding SelectedItem.Name, RelativeSource={RelativeSource Self}}" />
我有点好奇,你正在使用SelectedValue
。通常我使用SelectedItem
,但也可能有几种方法。