![](/img/trans.png)
[英]Understand Binding DataGridComboBoxColumn Itemssource and SelectedItem
[英]Binding to SelectedItem with a referenced ItemsSource
我有一個不同的DataSource池。 我有面具。 面具有索引線。 每個Indexline都有一個來自相關池的DataSource:
public class DataSource
{
public string Name { get; set; }
public override string ToString()
{
return Name;
}
}
public class Mask
{
public string Name { get; set; }
public ObservableCollection<Indexline> Indexlines { get; set; }
public override string ToString()
{
return Name;
}
}
public class Indexline
{
public DataSource SelectedDatasource { get; set; }
}
在我的MainWindow上,我有一些依賴屬性(對它們沒什么特別的):
ObservableCollection<DataSource>
) ObservableCollection<Mask>
) Mask
) 這是我的示例數據,它在Window
的Loaded
事件中設置:
this.AvalibleMasks = new ObservableCollection<Mask>()
{
new Mask()
{
Name = "Search Mask",
Indexlines = new ObservableCollection<Indexline>()
{
new Indexline(),
new Indexline(),
new Indexline(),
new Indexline(),
}
},
new Mask()
{
Name = "Document Mask",
Indexlines = new ObservableCollection<Indexline>()
{
new Indexline(),
new Indexline(),
}
}
};
this.AvalibleDataSources = new ObservableCollection<DataSource>()
{
new DataSource(){Name = "ERP Database"},
new DataSource(){Name = "CRM Database"},
};
這是我窗口的xaml代碼:
<Window x:Class="DataSourcesQuestion.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="MainWindow_instance"
Title="MainWindow" Height="372" Width="735" Loaded="Window_Loaded">
<Grid>
<ListBox ItemsSource="{Binding AvalibleMasks}" SelectedItem="{Binding SelectedMask}" Margin="10,10,10,236" />
<DataGrid Margin="10,111,10,43" ItemsSource="{Binding SelectedMask.Indexlines}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn Width="500" Header="Selected DataSource">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding AvalibleDataSources,Source={x:Reference MainWindow_instance}}"
SelectedItem="{Binding SelectedDatasource}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
我現在在ListBox
選擇一個掩碼,然后在DataGrid
中顯示所有索引行。 到目前為止一切都很好。 當我知道從ComboBox
選擇一個DataSource
,它不會存儲到Indexline
對象中。 (因為當我切換掩碼,然后切換回來時,選擇消失了。當我使用調試器時,我可以看到Mask
的SelectedDatasource
都是null)
這種行為的原因是什么? 我需要做什么改變來獲得預期的?
有人可以提出更好的頭銜嗎? 我覺得當前的一個不是很有幫助:(
我找到了原因:
ComboBox
的SelectedItem
屬性的默認UpdateSourceTrigger
似乎是Explicit
。 明確地將其設置為PropertyChanged
,解決了問題! 太簡單!
這就是新的完整XAML代碼
<Window x:Class="DataSourcesQuestion.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="MainWindow_instance"
Title="MainWindow" Height="372" Width="735" Loaded="Window_Loaded">
<Grid>
<ListBox ItemsSource="{Binding AvalibleMasks}" SelectedItem="{Binding SelectedMask}" Margin="10,10,10,236" />
<DataGrid Margin="10,111,10,43" ItemsSource="{Binding SelectedMask.Indexlines}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn Width="500" Header="Selected DataSource">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding AvalibleDataSources,Source={x:Reference MainWindow_instance}}"
SelectedItem="{Binding SelectedDatasource, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.