Wpf 在上面的示例中,路径较短,但在我的实际应用程序中,我需要访问对象的其他属性,并有可能更改它们的显示方式。正如Icrellik所说,它并不能解决问题。@wilford,我已经更新了答案。你能用SelectedItem而不是SelectedValue来测试你的
Wpf 在上面的示例中,路径较短,但在我的实际应用程序中,我需要访问对象的其他属性,并有可能更改它们的显示方式。正如Icrellik所说,它并不能解决问题。@wilford,我已经更新了答案。你能用SelectedItem而不是SelectedValue来测试你的,wpf,combobox,Wpf,Combobox,在上面的示例中,路径较短,但在我的实际应用程序中,我需要访问对象的其他属性,并有可能更改它们的显示方式。正如Icrellik所说,它并不能解决问题。@wilford,我已经更新了答案。你能用SelectedItem而不是SelectedValue来测试你的原始代码吗?我试过了,但仅仅用SelectedItem替换SelectedValue并不能改变SelectionBoxItemTemplate被用于选择而不是ItemTemplate(我期望并忽略了检查)的事实,因此行为是相同的。我只有对Sel
在上面的示例中,路径较短,但在我的实际应用程序中,我需要访问对象的其他属性,并有可能更改它们的显示方式。正如Icrellik所说,它并不能解决问题。@wilford,我已经更新了答案。你能用
SelectedItem
而不是SelectedValue
来测试你的原始代码吗?我试过了,但仅仅用SelectedItem替换SelectedValue并不能改变SelectionBoxItemTemplate被用于选择而不是ItemTemplate(我期望并忽略了检查)的事实,因此行为是相同的。我只有对SelectionBoxItemTemplate的读取权限,所以这就是问题所在。从我所读到的SelectedValue和SelectedItem之间的区别来看,我认为我在这里使用哪一个没有什么区别。@wilford,好的,我明白了,现在我看到它发生了,因为您的组合框中有groupbox
。我以前没提过。这取决于你,但我会尽量避免这种情况。回答很好,解释得很好,你的修复工作完美,谢谢!
<ComboBox ItemsSource="{Binding GroupBoxes}" SelectedValue="{Binding SelectedGroupBox}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
public MainWindow() {
InitializeComponent();
this.DataContext = this;
this.GroupBoxes = new ObservableCollection<GroupBox>();
this.GroupBoxes.Add(new GroupBox() { Name = "AAA", Header = "AAA", Height = 100, Background = Brushes.Purple });
this.GroupBoxes.Add(new GroupBox() { Name = "BBB", Header = "BBB", Height = 100, Background = Brushes.Purple });
this.GroupBoxes.Add(new GroupBox() { Name = "CCC", Header = "CCC", Height = 100, Background = Brushes.Purple });
this.GroupBoxes.Add(new GroupBox() { Name = "DDD", Header = "DDD", Height = 100, Background = Brushes.Purple });
this.GroupBoxes.Add(new GroupBox() { Name = "EEE", Header = "EEE", Height = 100, Background = Brushes.Purple });
}
#region GroupBoxesProperty
public static readonly DependencyProperty GroupBoxesProperty = DependencyProperty.Register(
"GroupBoxes", typeof(ObservableCollection<GroupBox>), typeof(MainWindow)
);
public ObservableCollection<GroupBox> GroupBoxes {
get { return (ObservableCollection<GroupBox>)GetValue(GroupBoxesProperty); }
set { SetValue(GroupBoxesProperty, value); }
}
#endregion
#region SelectedGroupBoxProperty
public static readonly DependencyProperty SelectedGroupBoxProperty = DependencyProperty.Register(
"SelectedGroupBox", typeof(GroupBox), typeof(MainWindow),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, (s, e) => (s as MainWindow).OnSelectedGroupBoxChanged())
);
public GroupBox SelectedGroupBox {
get { return (GroupBox)GetValue(SelectedGroupBoxProperty); }
set { SetValue(SelectedGroupBoxProperty, value); }
}
void OnSelectedGroupBoxChanged() {
Console.WriteLine("selection is now " + this.SelectedGroupBox.Name);
}
#endregion
<Combobox DisplayMemberPath="Name" ... />
<ContentPresenter IsHitTestVisible="false"
Margin="8,1,1,1"
Content="{TemplateBinding SelectionBoxItem}"
ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
<ComboBox ItemsSource="{Binding GroupBoxes}" SelectedValue="{Binding SelectedGroupBox}">
<ComboBox.ItemTemplateSelector>
<ts:ComboBoxItemTemplateSelector>
<ts:ComboBoxItemTemplateSelector.SelectedTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ts:ComboBoxItemTemplateSelector.SelectedTemplate>
</ts:ComboBoxItemTemplateSelector>
</ComboBox.ItemTemplateSelector>
</ComboBox>
public class ComboBoxItemTemplateSelector : DataTemplateSelector
{
public DataTemplate SelectedTemplate { get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
return SelectedTemplate;
}
}