我已经阅读了所有地方,WPF中的绑定是可行的接口,但我有一点时间实际上对它有任何牵引力。我也在使用EF Core,如果它可以帮助你准备我的代码。 ComboBox填充数据,因此数据绑定有效,但SelectedItem
无法绑定,所选项目中的文本显示为空白。
我没有得到以下内容,绑定到实现接口的对象。
ComboBox的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}"
/>
MainWindow.xaml.cs:
protected IData DB { get; private set; }
public MainWindow()
{
InitializeComponent();
DB = new Data.DataSQLite(true);
DB.Bind_JumpLists_ItemsSource(cbJumpList);
}
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; }
}
然后在实现的对象(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事件保持为空。
答案 0 :(得分:1)
SelectedItem
的{{1}}属性应该绑定到属性而不是类型。要使绑定生效,您还应将ComboBox
的{{1}}设置为定义此属性的类型的实例。
试试这个:
DataContext
ComboBox