我用ComboBox
Item Source
填充了List
。
我使用DisplayMemberPath="Name"
在下拉菜单中显示项目名称。
当我将自定义XAML ItemContainerStyle
应用于ComboBox
时,名称显示将停止工作。
无样式:
应用ItemContainerStyle:
组合框
<ComboBox x:Name="cboAudioSampleRate"
Style="{DynamicResource ComboBoxBlue}"
ItemContainerStyle="{DynamicResource ComboBoxBlueItem}" <-- Problem
ItemsSource="{Binding AudioSampleRate_Items}"
DisplayMemberPath="Name"
SelectedValuePath="Name"
SelectedIndex="{Binding AudioSampleRate_SelectedIndex}"
SelectedValue="{Binding AudioSampleRate_SelectedItem}"
IsEnabled="{Binding AudioSampleRate_IsEnabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Width="105"
Height="22"
SelectionChanged="cboAudioSampleRate_SelectionChanged"
ToolTip="Audio Sample Rate"
Grid.Column="2"
Grid.Row="6"
Margin="0,2,0,0"/>
查看模型
public class AudioSampleRate
{
public string Name { get; set; }
public string Frequency { get; set; }
}
public List<AudioSampleRate> _AudioSampleRate_Items = new List<AudioSampleRate>();
public List<AudioSampleRate> AudioSampleRate_Items
{
get { return _AudioSampleRate_Items; }
set
{
_AudioSampleRate_Items = value;
OnPropertyChanged("AudioSampleRate_Items");
}
}
商品来源列表
public static List<ViewModel.AudioSampleRate> sampleRate = new List<ViewModel.AudioSampleRate>()
{
new ViewModel.AudioSampleRate() { Name = "auto", Frequency = "" },
new ViewModel.AudioSampleRate() { Name = "8k", Frequency = "8000" },
new ViewModel.AudioSampleRate() { Name = "11.025k", Frequency = "11025" },
new ViewModel.AudioSampleRate() { Name = "12k", Frequency = "12000" },
new ViewModel.AudioSampleRate() { Name = "16k", Frequency = "16000" },
new ViewModel.AudioSampleRate() { Name = "22.05k", Frequency = "22050" },
new ViewModel.AudioSampleRate() { Name = "24k", Frequency = "24000" },
new ViewModel.AudioSampleRate() { Name = "32k", Frequency = "32000" },
new ViewModel.AudioSampleRate() { Name = "44.1k", Frequency = "44100" },
new ViewModel.AudioSampleRate() { Name = "48k", Frequency = "48000" }
};
商品样式
<!-- ComboBox Blue Item -->
<Style x:Key="ComboBoxBlueItem" TargetType="{x:Type ComboBoxItem}">
<Setter Property="Template" Value="{StaticResource ComboBoxBlue.Item.ControlTemplate}"/>
<Setter Property="Foreground" Value="#FFFFFFFF" />
<Setter Property="Background" Value="#FF1049BB" />
<Setter Property="BorderBrush" Value="#FF1049BB" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter Property="Background" Value="#FF1049BB" />
</Trigger>
</Style.Triggers>
</Style>
答案 0 :(得分:0)
我发现您不能同时使用DisplayMemberPath="Name"
和ItemContainerStyle
。
一种解决方法是将Bind
Name
固定到TextBlock
内的ComboBox
。
<ComboBox x:Name="cboSampleRate"
Style="{DynamicResource ComboBoxBlue}"
ItemContainerStyle="{DynamicResource ComboBoxBlueItem}"
ItemsSource="{Binding AudioSampleRate_Items}"
SelectedValuePath="Name"
SelectedIndex="{Binding AudioSampleRate_SelectedIndex}"
SelectedValue="{Binding AudioSampleRate_SelectedItem}"
IsEnabled="{Binding AudioSampleRate_IsEnabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Width="105"
Height="22"
SelectionChanged="cboSampleRate_SelectionChanged"
ToolTip="Audio Sample Rate"
Grid.Column="2"
Grid.Row="6"
Margin="0,2,0,0"
>
<ComboBox.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Name}"></TextBlock>
</Grid>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>