简体   繁体   English

将ComboBox项绑定到字符串

[英]Binding ComboBox item to string

I want to bind a ComboBox item to a string, but it does not work. 我想将一个ComboBox项绑定到一个字符串,但它不起作用。 My code is below. 我的代码如下。

Code in view: 代码在视图中:

<ComboBox          
 SelectedValuePath="content" 
 SelectedItem="{Binding ProductName}" 
            ......                       
 <ComboBoxItem>1111111111</ComboBoxItem>
 <ComboBoxItem>2222222222222</ComboBoxItem>
 <ComboBoxItem>333333333333</ComboBoxItem>
</ComboBox>

Code in view model: 视图模型中的代码:

private string _productName;
public string ProductName
{
    get { return _productName; }
    set
    {
        if (_productName != value)
        {
            _productName = value;
            RaisePropertyChangedEvent("ProductName");
        }
    } 
}

I assume you want to get the text from the ComboboxItem and not the ComboBoxItem iteself. 我假设你想从ComboboxItem而不是ComboBoxItem迭代获取文本。

So you are binding the wrong information. 所以你绑定了错误的信息。 This should work. 这应该工作。

<ComboBox          
SelectedValuePath="content" 
Text="{Binding ProductName}" 
            ......                       
<ComboBoxItem>1111111111</ComboBoxItem>
<ComboBoxItem>2222222222222</ComboBoxItem>
<ComboBoxItem>333333333333</ComboBoxItem>
</ComboBox>

Selected Item is of type ComboBoxItem, it will not accept String. Selected Item的类型为ComboBoxItem,它不接受String。 If you want to display product name in some other place try maybe something like this: 如果您想在其他地方显示产品名称,请尝试以下方法:

 <TextBox Text="{Binding ElementName=my_ComboBox, Path=SelectedItem}"/>

Just a suggestion. 只是一个建议。 You already use a binding for the SelectedItem , why don't you set up another binding for the Items using the ItemsSource ? 您已经使用了SelectedItem的绑定,为什么不使用ItemsSource为Items设置另一个绑定? So you would not need to add them statically in your view. 因此,您不需要在视图中静态添加它们。

In addition you would not have the trouble to wonder whether you deal with instances of ComboxItem or String with your SelectedItem binding. 此外,您不必怀疑是否使用SelectedItem绑定处理ComboxItemString实例。 In case of the binding via ItemsSource you can be sure that the SelectedItem is a string. 如果通过ItemsSource进行绑定,您可以确定SelectedItem是一个字符串。

Here is the code: 这是代码:

<ComboBox          
 SelectedValuePath="content" 
 SelectedItem="{Binding ProductName}"
 ItemsSource="{Binding ProductNames}"
</ComboBox>

In your view model (or code behind) you define the ProductNames : 在您的视图模型(或后面的代码)中,您可以定义ProductNames

public String[] ProductNames
{
    get
    {
        return _productNames;
    }
    set
    {
        if (_productNames!= value)
        {
            _productNames = value;
            RaisePropertyChangedEvent("ProductNames");
        }
    }
}
String[] _productNames;


public NameOfConstructor()
{
    List<String> productNames = new List<String>();
    productNames.Add("A");
    productNames.Add("B");
    productNames.Add("C");

    ProductNames = productNames.ToArray();
}

If it was possible that the list of names changes during execution, I would use a ObservableCollection<string> instead String[] . 如果在执行期间名称列表有可能发生变化,我会使用ObservableCollection<string>而不是String[]

It should be like this 它应该是这样的

  1. Create an observable collection of Product in View Model. 在View Model中创建一个可观察的Product集合。 Lets Say ProductCollection 让我们说ProductCollection
  2. Bind to the ComboBox ItemSource as given below 绑定到ComboBox ItemSource,如下所示
 <ComboBox Name="productComboBox" Width="200" Height="30" ItemsSource="{Binding ProductCollection}"> <ComboBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Path=ProductName}"></TextBlock> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox> 

if you want to show in textbox somewhere use this 如果你想在文本框中显示某个地方使用它

<TextBox Text="{Binding ElementName=productComboBox, Path=SelectedItem}"/>

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

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