[英]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
绑定处理ComboxItem
或String
实例。 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 它应该是这样的
<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.