我有一个ComboBox ,它有一个SortedList<int, double>作为ItemsSource ,它包含项目数和价格(推导相当复杂)。 用户可以选择项目数量,页面上的textbox显示这些项目的价格。 因此,我将DisplayMember设置为NumberOfItems,将ValueMember为Price。 这很好用。
但是,现在我想在Orders类中存储项目数; 不是价格! 我找不到任何这样的例子,只绑定到SelectedValue,这不是我想做的。 (我很清楚,这种组合框的使用完全是“另一种方式”:))
我尝试绑定到SelectedItem,但这似乎不起作用:
BindingOperations.SetBinding(NumberOfItemsCombo, ComboBox.SelectedItemProperty, new Binding("NumberOfItems") { Source = Order});是否可以将NumberOfItems绑定到类似“SelectedDisplayValue”的东西?
谢谢!
编辑:示例:Sortedlist包含以下值:
Index NumberOfItems Price [0]: {[1, 41]} [1]: {[2, 82]} [2]: {[3, 123]} [3]: {[4, 164]} [4]: {[5, 205]} [5]: {[6, 246]} [6]: {[7, 287]} [7]: {[8, 328]} [8]: {[9, 369]} [9]: {[10, 410]}ComboBox应该反映Column NumberOfItems(即1 - 10)。 如果用户选择一个值,比如7,则文本框显示示例287中的Price。订单类有一个属性NumberOfItems,我想绑定到ComboBox。 因此,一旦将属性设置为某个数字(例如6),组合框应显示6和文本框246。
编辑II:
感谢Liero,它现在在xaml中运行良好。 现在我只需要在Code中绑定Textbox。 但是,这不起作用:
BindingOperations.SetBinding(PriceTextBox, TextBox.TextProperty, new Binding() { Source=ComboBox, Path = new PropertyPath("SelectedItem.Price") });弄清楚:方向不见了。 现在它有效!
BindingOperations.SetBinding(PriceTextBox, TextBox.TextProperty, new Binding() { Source=ComboBox, Path = new PropertyPath("SelectedItem.Price"), Mode = BindingMode.OneWay });I've got a ComboBox which has a SortedList<int, double> as ItemsSource that contains the Number of Items and the Price (which is fairly complicated to deduce). The user can select the number of Items and a textbox on the page shows the price for these items. Hence, I set the DisplayMember to NumberOfItems and the ValueMember to Price. This works fine.
However, now I want to store the Number of Items in an Orders class; not the price! I couldn't find any examples of how to this, only binding to SelectedValue, which is not what I want to do. (I'am well aware, that this use of the combobox is quite the "other way around":) )
I tried binding to SelectedItem, but that doesn't seem to work:
BindingOperations.SetBinding(NumberOfItemsCombo, ComboBox.SelectedItemProperty, new Binding("NumberOfItems") { Source = Order});Is it possible to bind the NumberOfItems to the something like the "SelectedDisplayValue"?
Thanks!
Edit: Example: The Sortedlist contains the following values:
Index NumberOfItems Price [0]: {[1, 41]} [1]: {[2, 82]} [2]: {[3, 123]} [3]: {[4, 164]} [4]: {[5, 205]} [5]: {[6, 246]} [6]: {[7, 287]} [7]: {[8, 328]} [8]: {[9, 369]} [9]: {[10, 410]}The ComboBox should Dispay the Column NumberOfItems (ie 1 - 10). If the user selects a value, say 7, a Textbox displays the Price, in the example 287. The order class has a property NumberOfItems which I want to bind to ComboBox. So once the Property is set to a certain number, say 6, the combobox should display 6 and the textbox 246.
Edit II:
Thanks to Liero it works fine now in xaml. Now I only need to do the binding of the Textbox in Code. However, this is not working:
BindingOperations.SetBinding(PriceTextBox, TextBox.TextProperty, new Binding() { Source=ComboBox, Path = new PropertyPath("SelectedItem.Price") });Figured it out: The direction was missing. Now it works!
BindingOperations.SetBinding(PriceTextBox, TextBox.TextProperty, new Binding() { Source=ComboBox, Path = new PropertyPath("SelectedItem.Price"), Mode = BindingMode.OneWay });最满意答案
你想要替换SortedList,其中key是NumberOfItems,value是Price,更直观吗?
public class Order { public int NumberOfItems {get; set;} public double Price {get; set;} } comboBox1.ItemsSource = new List<Order> { new Order { NumberOfItems = 1, Price = 20 } new Order { NumberOfItems = 2, Price = 40 } } <ComboBox x:Name="comboBox1" DisplayMemberPath="NumberOfItems"/> <TextBlock Text="{Binding SelectedItem.Price, ElementName=comboBox1}" />Do you want to replace SortedList where key is NumberOfItems and value is Price with something more intuitive?
public class Order { public int NumberOfItems {get; set;} public double Price {get; set;} } comboBox1.ItemsSource = new List<Order> { new Order { NumberOfItems = 1, Price = 20 } new Order { NumberOfItems = 2, Price = 40 } } <ComboBox x:Name="comboBox1" DisplayMemberPath="NumberOfItems"/> <TextBlock Text="{Binding SelectedItem.Price, ElementName=comboBox1}" />更多推荐
发布评论