我有一个ComboBox,我想用enum的成员填充,带有本地化的代表字符串。我知道执行此操作的标准方法是在代码隐藏中创建一个字典,其中枚举值为键,文本为值,然后将ItemsSource设置为该值。但后来我无法使用我性感的MarkupExtension。所以,我想在XAML中这样做。我觉得这很容易;这就是我所拥有的:
<ComboBox x:Name="cmbNewTabPos"
DisplayMemberPath="Content"
SelectedValue="{Binding Path=NewTabPosition}"
SelectedValuePath="Tag">
<ComboBoxItem
Content="{qt:Resx Key=SomeKey, Index=0}"
Tag="{x:Static qt:TabPos.Left}"/>
<ComboBoxItem
Content="{qt:Resx Key=SomeKey, Index=1}"
Tag="{x:Static qt:TabPos.Right}"/>
<ComboBoxItem
Content="{qt:Resx Key=SomeKey, Index=2}"
Tag="{x:Static qt:TabPos.Leftmost}"/>
<ComboBoxItem
Content="{qt:Resx Key=SomeKey, Index=3}"
Tag="{x:Static qt:TabPos.Rightmost}"/>
</ComboBox>
它几乎可以工作;正确填充下拉列表,绑定工作正常,当我下拉下拉列表时,我可以看到所选值,但无论我做什么,组合框的框部分都保持空白。我在这里做错了什么?
答案 0 :(得分:3)
我写了这个小例子,它运作正常。
<Window x:Class="MainWindowCommandBinding.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">
<Grid>
<Grid.Resources >
<Point x:Key="1_2" X="1" Y="2"/>
<Point x:Key="1_3" X="1" Y="3"/>
<Point x:Key="1_4" X="1" Y="4"/>
<Point x:Key="1_5" X="1" Y="5"/>
</Grid.Resources>
<ComboBox x:Name="cmbNewTabPos"
DisplayMemberPath="Y"
SelectedValuePath="Tag"
SelectedValue="1"
Margin="0,12,0,0" HorizontalAlignment="Left" Width="135" Height="37" VerticalAlignment="Top">
<ComboBoxItem Content="{StaticResource ResourceKey=1_2}" Tag="1"/>
<ComboBoxItem Content="{StaticResource ResourceKey=1_3}" Tag="2"/>
<ComboBoxItem Content="{StaticResource ResourceKey=1_4}" Tag="3"/>
<ComboBoxItem Content="{StaticResource ResourceKey=1_5}" Tag="4"/>
</ComboBox>
</Grid>
我认为你没有正确使用DisplayeMemberPath="Content"
。这用于指定从所选对象显示的值。所选对象不是选定的ComboBoxItem,而是所选ComboBoxItem的Content
属性中的内容。但是从你的代码我可以看到你的ComboBoxItems中的对象只有两个名为"Key"
和"Index"
的属性。
希望这有帮助。如果我误解了,请告诉我。