Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何绑定组合框';是否相对于其ID而不是对象本身来设置选定项?_C#_Entity Framework_Xaml_Data Binding_Combobox - Fatal编程技术网

C# 如何绑定组合框';是否相对于其ID而不是对象本身来设置选定项?

C# 如何绑定组合框';是否相对于其ID而不是对象本身来设置选定项?,c#,entity-framework,xaml,data-binding,combobox,C#,Entity Framework,Xaml,Data Binding,Combobox,考虑以下标记(最后三个标签仅用于开发目的,以便我可以验证绑定是否按照我的想法进行) 果不其然,它是有效的,这进一步表明我的结论是正确的。现在我有两个问题 是否有方法从标记进行比较(即不覆盖相等检查) 我是否应该担心由于这些对象是不同的实例而导致比较失败(风险) Selector及其子体不喜欢非null的SelectedItem不在ItemsSource中 假设您正在使用视图模型,则在加载视图模型时,最好将Order.TimeFrame与TimeFrame同步。即: if (Order.TimeF

考虑以下标记(最后三个标签仅用于开发目的,以便我可以验证绑定是否按照我的想法进行)

果不其然,它是有效的,这进一步表明我的结论是正确的。现在我有两个问题

  • 是否有方法从标记进行比较(即不覆盖相等检查)
  • 我是否应该担心由于这些对象是不同的实例而导致比较失败(风险)

  • Selector
    及其子体不喜欢非null的
    SelectedItem
    不在
    ItemsSource

    假设您正在使用视图模型,则在加载视图模型时,最好将
    Order.TimeFrame
    TimeFrame
    同步。即:

    if (Order.TimeFrame != null)
    {
        Order.TimeFrame = TimeFrames.SingleOrDefault(_ => _.Id == Order.TimeFrame.Id);
    }
    

    Order.TimeFrame.Id
    不在
    TimeFrames
    Id中时,这也将正确处理该情况。

    我认为您应该为所选的TimeFrame添加一个属性并绑定到它

    <ComboBox x:Name="TimeFrame"
          DisplayMemberPath="Name"
          ItemsSource="{Binding TimeFrames}"
          SelectedItem="{Binding Path=SelectedTimeFrame }"
          SelectionChanged="Combox_OnSelectionChanged" />
    
    
    
    在属性设置器中,更新Order.TimeFrame

    if (Order.TimeFrame != null)
    {
        Order.TimeFrame = TimeFrames.SingleOrDefault(_ => _.Id == Order.TimeFrame.Id);
    }
    
    <ComboBox x:Name="TimeFrame"
          DisplayMemberPath="Name"
          ItemsSource="{Binding TimeFrames}"
          SelectedItem="{Binding Path=SelectedTimeFrame }"
          SelectionChanged="Combox_OnSelectionChanged" />