简体   繁体   English

WPF ComboBox 将文本绑定到所选项目

[英]WPF ComboBox bind text to selected item

I try to bind a ComboBox to a collection:我尝试将ComboBox绑定到一个集合:

<ComboBox  Margin="4 0 2 0"
    ItemsSource="{Binding YAxes}"
    SelectedItem="{Binding SelectedYAxis, Mode=TwoWay}"
    DisplayMemberPath="AxisTitle"
    SelectedValuePath="AxisTitle"/>

Everything is fine, except Text of this ComboBox .一切都很好,除了这个ComboBox Text On selection of item, the setter on SelectedYAxis fires and notifies, that property has been changed:在选择项目时, SelectedYAxis上的 setter 会触发并通知该属性已更改:

private IAxis _selectedYAxis;
    public IAxis SelectedYAxis
    {
        get => _selectedYAxis;
        set
        {
            _selectedYAxis = value;
            OnPropertyChanged(nameof(SelectedYAxis));
        }
    }

but the text on ComboBox never changes to the selected items AxisTitle .ComboBox上的文本永远不会更改为所选项目AxisTitle How to display an AxisTitle of SelectedItem as a text of ComboBox ?如何将SelectedItemAxisTitle显示为ComboBox的文本?

UPD : Text is never shown, even if it's set explicitly: UPD :文本永远不会显示,即使它被明确设置:

<ComboBox  Margin="4 0 2 0"
    ItemsSource="{Binding XAxes}"
    SelectedItem="{Binding SelectedXAxis, Mode=TwoWay}"
    DisplayMemberPath="AxisTitle"
    Text="Asdasd"/>

It doesn't set the text of ComboBox to "Asdasd".它不会将ComboBox的文本设置为“Asdasd”。

UPD 2 : I've changed the things to use DataTemplate, but this didn't work as well: UPD 2 :我已经改变了使用 DataTemplate 的东西,但这并没有奏效:

<ComboBox  Margin="4 0 2 0"
           ItemsSource="{Binding YAxes}"
           SelectedItem="{Binding SelectedYAxis, Mode=TwoWay}"
           ItemTemplate="{StaticResource AxisCBTextTemplate}"/>

And the resource section above:以及上面的资源部分:

<DataTemplate x:Key="AxisCBTextTemplate">
    <TextBlock Text="{Binding AxisTitle}"/>
</DataTemplate>

UPD 3 An illustration to what do I mean: UPD 3说明我的意思:

ComboBox 文本永远不会更新

The task of displaying some selected text should be trivial, but it has difficulties.显示一些选定文本的任务应该是微不足道的,但它有困难。

I've found the root cause of this issue.我已经找到了这个问题的根本原因。 Each IAxis (it is a SciChart axis object) from XAxes and YAxes is already dispayed on the graph (ie bound).来自XAxesYAxes每个IAxis (它是一个SciChart轴对象)已经在图形上显示(即绑定)。 Binding them to other controls (like ListBox) causes an exception: "Must disconnect specified child from current parent Visual before attaching to new parent Visual.", I found it out while trying to bind them to ListBox.将它们绑定到其他控件(如 ListBox)会导致异常:“在附加到新的父 Visual 之前,必须断开指定的子级与当前父 Visual 的连接。”,我在尝试将它们绑定到 ListBox 时发现了这一点。

Seemes like ComboBox catches such exceptions and doesn't output StackTrace for any case.似乎 ComboBox 会捕获此类异常,并且在任何情况下都不输出 StackTrace。 In my case this exception was wrapped into NullReferenceException and occurred only on click on a ComboBox, that has no ItemTemplate set.在我的情况下,此异常被包装到NullReferenceException并且仅在单击未设置ItemTemplate的 ComboBox 时发生。 Though I may not be fully correct in details, replacing XAxes and YAxes with collections of strings solves this issue.虽然我在细节上可能不完全正确,但用字符串集合替换XAxesYAxes解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM