C# 在WPF中为组合框SelectedItem绑定接口

C# 在WPF中为组合框SelectedItem绑定接口,c#,wpf,xaml,interface,entity-framework-core,C#,Wpf,Xaml,Interface,Entity Framework Core,我到处都读过,在WPF中绑定到接口是可行的,但我有一段很长的时间实际上得到了任何牵引力。我也在使用EF Core,如果它能帮助您准备好我的代码的话。组合框中填充了数据,因此数据的绑定工作正常,但SelectedItem无法绑定,所选项目中的文本显示为空白 我不明白下面的代码是如何绑定到实现接口的对象的 组合框的XAML: <ComboBox Height="23" x:Name="cbJumpList" Width="177" Margin="2" HorizontalAlignment=

我到处都读过,在WPF中绑定到接口是可行的,但我有一段很长的时间实际上得到了任何牵引力。我也在使用EF Core,如果它能帮助您准备好我的代码的话。组合框中填充了数据,因此数据的绑定工作正常,但SelectedItem无法绑定,所选项目中的文本显示为空白

我不明白下面的代码是如何绑定到实现接口的对象的

组合框的XAML:

<ComboBox Height="23" x:Name="cbJumpList" Width="177" Margin="2" HorizontalAlignment="Left"
            IsEditable="False"
            DisplayMemberPath="Name"
            SelectedItem="{Binding Path=(model:IData.SelectedJumpList), Mode=TwoWay}"
            />
IData.cs:

public interface IData : IDisposable, INotifyPropertyChanged
{
    void Bind_JumpLists_ItemsSource(ItemsControl control);
    IJumpList First_JumpList();

    IJumpList SelectedJumpList { get; set; } // TwoWay Binding
}
IJumpList.cs

public interface IJumpList
{
    long JumpListId { get; set; }
    string Name { get; set; }
}
然后在实现的object Data.DataSQLite中:

public void Bind_JumpLists_ItemsSource(ItemsControl control)
{
    control.ItemsSource = null;

    db.JumpLists.ToList();
    control.ItemsSource = db.JumpLists.Local;
    control.Tag = db.JumpLists.Local;

    SelectedJumpList = db.JumpLists.FirstOrDefault();
}

public IJumpList SelectedJumpList
{
    get { return _SelectedJumpList; }
    set
    {
        _SelectedJumpList = value;
        NotifyPropertyChanged();
    }
}
IJumpList _SelectedJumpList;

private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
我要补充的是,PropertyChanged事件仍然为空。

组合框的SelectedItem属性应该绑定到属性而不是类型。要使绑定工作,还应将ComboBox的DataContext设置为定义此属性的类型的实例

试试这个:

<ComboBox Height="23" x:Name="cbJumpList" Width="177" Margin="2" HorizontalAlignment="Left"
          IsEditable="False"
          DisplayMemberPath="Name"
          SelectedItem="{Binding SelectedJumpList}" />

正在尝试将从ToList返回的列表绑定到SelectedItem属性?IJumpList中还有什么?@SivaGopal否,ToList正在更新其他绑定中使用的Items.Local属性。我想那可能会引起混乱,我会把它删掉。谢谢。control.DataContext=this;在类型的实例上是键。更改SelectedItem并不重要。你的方法奏效了,我也一样。现在我必须努力让它更新我的另一个列表,改变它会影响到我,但这是一个很大的障碍。真不敢相信有人否决了它,因为它是解决方案。
<ComboBox Height="23" x:Name="cbJumpList" Width="177" Margin="2" HorizontalAlignment="Left"
          IsEditable="False"
          DisplayMemberPath="Name"
          SelectedItem="{Binding SelectedJumpList}" />
public void Bind_JumpLists_ItemsSource(ItemsControl control)
{
    db.JumpLists.ToList();
    control.DataContext = this;
    control.ItemsSource = db.JumpLists.Local;
    control.Tag = db.JumpLists.Local;

    SelectedJumpList = db.JumpLists.FirstOrDefault();
}