C# 显示数据绑定 WPF 组合框的默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1910896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 21:46:42  来源:igfitidea点击:

Display a Default value for a Databound WPF ComboBox

c#wpfdata-bindingcombobox

提问by Orion Edwards

I have a databound WPF comboxbox where I am using the SelectedValuePathproperty to select a selected value based on something other than the object's text. This is probably best explained with an example:

我有一个数据绑定 WPF comboxbox,我在其中使用该SelectedValuePath属性根据对象文本以外的其他内容选择选定的值。这可能最好用一个例子来解释:

<ComboBox ItemsSource="{Binding Path=Items}"
          DisplayMemberPath="Name"
          SelectedValuePath="Id"
          SelectedValue="{Binding Path=SelectedItemId}"/>

The datacontext for this thing looks like this:

这个东西的数据上下文看起来像这样:

DataContext = new MyDataContext
{
    Items = {
        new DataItem{ Name = "Jim", Id = 1 },
        new DataItem{ Name = "Bob", Id = 2 },
    },
    SelectedItemId = -1,
};

This is all well and good when I'm displaying pre-populated data, where the SelectedItemIdmatches up with a valid Item.Id.

当我显示预先填充的数据时,这一切都很好,其中SelectedItemId与有效的Item.Id.

The problem is, in the new itemcase, where the SelectedItemIdis unknown. What WPF does is show the combo box as blank. I do not want this. I want to disallow blank items in the combo box; I would like it to display the first item in the list.

问题是,在新项目的情况下,SelectedItemId未知。WPF 所做的是将组合框显示为空白。我不想要这个。我想禁止组合框中的空白项目;我希望它显示列表中的第一项。

Is this possible? I could write some code to explicitly go and set the SelectedItemIdbeforehand, but it doesn't seem right to have to change my data model because of a shortcoming in the UI.

这可能吗?我可以编写一些代码来明确地SelectedItemId预先设置并设置,但由于 UI 中的缺陷而不得不更改我的数据模型似乎并不正确。

采纳答案by Ben Collier

I think you are going to have to do some manual work here to get this behavior. You could check in code behind when you first display the ComboBox whether or not the SelectedItemId matches up or not and then change the selected index based on that. Or if you know that the SelectedItemId will always be -1 when there is no corresponding item, you could use a datatrigger.

我认为您将不得不在这里做一些手动工作才能获得这种行为。无论 SelectedItemId 是否匹配,您都可以在首次显示 ComboBox 时签入代码,然后根据此更改所选索引。或者,如果您知道在没有相应项目时 SelectedItemId 将始终为 -1,则可以使用数据触发器。

Method 1:

方法一:

if (!DataContext.Items.Exists(l => l.Id == DataContext.SelectedItemId))
{
    MyComboBox.SelectedIndex = 0;  //this selects the first item in the list
}

Method 2:

方法二:

<Style TargetType="ComboBox">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=SelectedItemId}" Value="-1">
            <Setter Property="SelectedIndex" Value="0"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

回答by MBDevelop

you may use this style trigger: if selecteditem is null, first element is selected.

您可以使用这种样式触发器:如果 selecteditem 为空,则选择第一个元素。

<Trigger Property="SelectedItem" Value="{x:Null}">
    <Setter Property="SelectedIndex" Value="0"/>
</Trigger>