我需要將所選專案系結到一個屬性,但不知何故它不起作用。
這是我的嘗試:
<ItemsControl ItemsSource="{Binding MyItems}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<Label Content="{Binding FirstProperty}"/>
<ComboBox ItemsSource="{Binding SecondProperties}" SelectedItem="{Binding SelectedProperty}" />
...
這是執行此操作的模型:
private List<MyItems> _myItems;
public List<MyItem> MyItems{
get => _myItems;
set
{
_myItems= value;
OnPropertyChanged();
}
}
這是我的課:
public class MyProperty
{
public List<string> SecondProperties{ get; set; }
public string FirstProperty;
public string SelectedProperty;
}
我希望SelectedProperty匹配我選擇的內容。當我以后使用它時,FirstProperty很好,組合框串列SecondProperties也很好,但SelectedProperty總是null.
有什么幫助嗎?
uj5u.com熱心網友回復:
SelectedProperty 必須定義為公共屬性:
public string SelectedProperty { get; set; }
這是一個公共領域:
public string SelectedProperty;
uj5u.com熱心網友回復:
我修復了你的代碼:
public class MyProperty : INotifyPropertyChanged
{
private string selectedProperty;
public string SelectedProperty
{
get
{
return selectedProperty;
}
set
{
selectedProperty = value;
OnPropertyChanged("SelectedProperty");}
}
protected virtual void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
在您的 xaml 中添加 Mode=TwoWay
<ComboBox ItemsSource="{Binding SecondProperties}" SelectedItem="{Binding SelectedProperty, Mode=TwoWay }" />
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/423091.html
標籤:
