如何在ComboBox中显示当前选定的项目?选择项目的信息存储在XML文件中。
它在应用程序启动时的外观如何:
应用程序启动应如何看待:
XML数据源如下所示:
<Contact>
<Name>John</Name>
<Lastname>Doe</Lastname>
<Gender>Male</Gender>
</Contact>
这是我尝试的(在许多其他变体中)
<ComboBox SelectedItem="{Binding XPath=Contact/Gender, Mode=TwoWay}" Name="cmbGender" Width="100" >
<ComboBoxItem Content="Male" />
<ComboBoxItem Content="Female" />
</ComboBox>
我猜这是没有XmlDataProvider的方式。有没有办法用XPath表达式设置IsSelected?
<ComboBox Name="cmbGender" Width="100" >
<ComboBoxItem Content="Male" IsSelected="True"/>
<ComboBoxItem Content="Female" />
</ComboBox>
修改 这就是我设置数据源的方式:
<Grid>
<Grid.DataContext>
<XmlDataProvider x:Name="DataProvider" XPath="/" />
</Grid.DataContext>
// Binding is working fine
<TextBox Name="txtLastname" Width="100" Text="{Binding XPath=Contact/Lastname, UpdateSourceTrigger=PropertyChanged}" />
// not working
<ComboBox SelectedItem="{Binding XPath=Contact/Gender, Mode=TwoWay}" Name="cmbGender" Width="100" >
<ComboBoxItem Content="Male" />
<ComboBoxItem Content="Female" />
</ComboBox>
</Grid>
答案 0 :(得分:1)
文本属性是关键,这是它的工作原理:
<ComboBox Text="{Binding XPath=Contact/Gender, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Name="cmbGender" Width="100">
<ComboBoxItem Content="Male" />
<ComboBoxItem Content="Female" />
</ComboBox>