您可以覆盖ItemTemplate
and ItemContainerStyle
,这样下拉列表中的 Items 将正确显示,但 ComboBox 中的 Item 将是"[Selected Table]"
例子:
<ComboBox ItemsSource="{Binding Items}">
<ComboBox.ItemTemplate>
<!-- The template for the SelectedItem -->
<DataTemplate>
<TextBlock Text="[Selected Table]" />
</DataTemplate>
</ComboBox.ItemTemplate>
<ComboBox.ItemContainerStyle>
<!-- The style for the dropdown items -->
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="Template">
<Setter.Value>
<!-- Add dataTemplate here if you are binding to complex object -->
<ControlTemplate TargetType="{x:Type ContentControl}">
<TextBlock Text="{Binding}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ComboBox.ItemContainerStyle>
</ComboBox>
工作副本:
xml:
<Window x:Class="WpfApplication9.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="132" Width="278" Name="UI">
<StackPanel DataContext="{Binding ElementName=UI}">
<ComboBox ItemsSource="{Binding Items}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="[Selected Table]" />
</DataTemplate>
</ComboBox.ItemTemplate>
<ComboBox.ItemContainerStyle>
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ContentControl}">
<TextBlock Text="{Binding}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ComboBox.ItemContainerStyle>
</ComboBox>
</StackPanel>
</Window>
代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Items.Add("Item1");
Items.Add("Item2");
Items.Add("Item3");
Items.Add("Item4");
}
private ObservableCollection<string> _items = new ObservableCollection<string>();
public ObservableCollection<string> Items
{
get { return _items; }
set { _items = value; }
}
}
结果:
下拉:已选择:
编辑:
也许这个?
<Window x:Class="WpfApplication17.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication17"
Title="MainWindow" Height="350" Width="500" Name="UI">
<StackPanel DataContext="{Binding ElementName=UI}">
<ComboBox x:Name="combo" ItemsSource="{Binding Items}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Text" Value="{Binding}" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsDropDownOpen, ElementName=combo}" Value="false">
<Setter Property="Text" Value="{Binding MyText, ElementName=UI}" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>
代码:
public partial class MainWindow : Window, INotifyPropertyChanged
{
private string _myText;
private ObservableCollection<string> _items = new ObservableCollection<string>();
public MainWindow()
{
InitializeComponent();
Items.Add("Item1");
Items.Add("Item2");
Items.Add("Item3");
Items.Add("Item4");
MyText = "StackOverflow";
}
public string MyText
{
get { return _myText; }
set { _myText = value; OnPropertyChanged("MyText"); }
}
public ObservableCollection<string> Items
{
get { return _items; }
set { _items = value; }
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string e)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(e));
}
}