将ComboBox绑定到Displaymember到Object

时间:2016-07-06 08:00:45

标签: c# wpf combobox

我的SortedList<int, double> ItemsSource textboxDisplayMember,其中包含项目数和价格(推理相当复杂)。用户可以选择项目数量,页面上的ValueMember显示这些项目的价格。因此,我将BindingOperations.SetBinding(NumberOfItemsCombo, ComboBox.SelectedItemProperty, new Binding("NumberOfItems") { Source = Order}); 设置为NumberOfItems,将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]} 设置为Price。这很好用。

但是,现在我想在Orders类中存储项目数;不是价格!我找不到任何这样的例子,只能绑定到SelectedValue,这不是我想要做的。 (我很清楚,这种组合框的使用完全是&#34;其他方式&#34; :))

我尝试绑定到SelectedItem,但这似乎不起作用:

BindingOperations.SetBinding(PriceTextBox, TextBox.TextProperty, new Binding() { Source=ComboBox, Path = new PropertyPath("SelectedItem.Price") });

是否可以将NumberOfItems绑定到&#34; SelectedDisplayValue&#34;?

之类的东西

谢谢!

编辑:示例: Sortedlist包含以下值:

BindingOperations.SetBinding(PriceTextBox, TextBox.TextProperty, new Binding() { Source=ComboBox, Path = new PropertyPath("SelectedItem.Price"), Mode = BindingMode.OneWay });

ComboBox应该反映Column NumberOfItems(即1 - 10)。如果用户选择值,例如7,则文本框显示示例287中的Price。 订单类有一个属性NumberOfItems,我想绑定到ComboBox。因此,一旦将属性设置为某个数字(例如6),组合框应显示6和文本框246.

编辑II:

感谢Liero,它现在在xaml中工作正常。现在我只需要在Code中绑定Textbox。但是,这不起作用:

Microsoft.Deployment.WindowsInstaller.dll

想出来:方向不见了。现在它有效!

[CustomAction]
public static ActionResult IsExcelRunning(Session session)
{
    session.Log("Begin IsExcelRunning.");

    MessageResult msgBoxResult = MessageResult.Cancel;
    do
    {
        session.Log("Try to find running Excel.");

        bool isExcelRunning = false;
        try
        {
            var obj = Marshal.GetActiveObject("Excel.Application");
            isExcelRunning = null != obj;
            session.Log("IsExcelRunning = " + isExcelRunning);

            if (null != obj)
            {
                Marshal.FinalReleaseComObject(obj);
                obj = null;
                GC.Collect();
            }
        }
        catch (Exception ex)
        {
            session.Log("Exception:" + ex);
        }

        if (!isExcelRunning)
        {
            session.Log("End IsExcelRunning.");
            return ActionResult.Success;
        }
        else
        {
            var errorRecord = new Record(1);
            errorRecord.SetInteger(1, 30000);
            try
            {
                msgBoxResult =
                    session.Message(InstallMessage.Error | (InstallMessage) MessageButtons.RetryCancel,
                        errorRecord);

            }
            catch (InstallCanceledException)
            {
                var questionRecord = new Record(1);
                questionRecord.SetInteger(1, 1602);

                if (MessageResult.Yes ==
                    session.Message(
                        InstallMessage.Error | (InstallMessage)MessageButtons.YesNo |
                        (InstallMessage)MessageBoxIcon.Question, questionRecord))
                {
                    session.Log("End SetIsExcelRunning.");
                    return ActionResult.UserExit;
                }
                else
                {
                    msgBoxResult = MessageResult.Retry;
                }
            }
            catch (Exception ex)
            {
                session.Log("Unexpected exception:" + ex);
                throw;
            }
        }
    } while (MessageResult.Retry == msgBoxResult);

    session.Log("End IsExcelRunning.");
    return ActionResult.UserExit;
}

1 个答案:

答案 0 :(得分:1)

你想替换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}" />