我最近發現了索引屬性。這看起來像是完美的解決方案,在這種情況下,我所使用的數據最好在集合中表達,但仍需要作爲可用於XAML數據綁定的屬性來實現。我只是開始創建索引屬性的測試,並且我在那裏沒有問題,但我似乎無法使綁定工作。C#WPF綁定到索引屬性 - 我做錯了什麼?
任何人都可以指出我要去哪裏錯了嗎?
下面是測試類與嵌套類創建索引屬性:
public class TestListProperty
{
public readonly ListProperty ListData;
//---------------------------
public class ListProperty : INotifyPropertyChanged
{
private List<string> m_data;
internal ListProperty()
{
m_data = new List<string>();
}
public string this[int index]
{
get
{
if (m_data.Count > index)
{
return m_data[index];
}
else
{
return "Element not set for " + index.ToString();
}
}
set
{
if (m_data.Count > index)
{
m_data[index] = value;
}
else
{
m_data.Insert(index, value);
}
OnPropertyChanged("Item[]");
Console.WriteLine(value);
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
//---------------------------
public TestListProperty()
{
ListData = new ListProperty();
}
}
這裏是XAML:
<Window x:Class="TestIndexedProperties.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBox Width="200" Grid.Row="0" Text="{Binding Path=ListData[0], Mode=TwoWay}" />
<TextBox Width="200" Grid.Row="1" Text="{Binding Path=ListData[1], Mode=TwoWay}" />
<TextBox Width="200" Grid.Row="2" Text="{Binding Path=ListData[2], Mode=TwoWay}" />
</Grid>
</Window>
這裏是窗口代碼:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
TestListProperty test = new TestListProperty();
this.DataContext = test;
test.ListData[0] = "ABC";
test.ListData[1] = "Pleeez 2 wurk?";
test.ListData[2] = "Oh well";
}
}
感謝您的幫助!
運行時在輸出窗口中發生任何綁定錯誤/警告? –