我有一个包含2列的数据网格 一个是DataGridText 第二个是DataGridComboBox 当我加载数据时,两列都被归档,但是当我单击第二列时,我想要一个组合框将从第二个列表中加载数据 所以列表是这样的
公共ObservableCollection PriorityRuleList {get;组; } = new ObservableCollection();
public ObservableCollection<int> RulePriorityDefaultList { get; set; } = new ObservableCollection<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
PriorityRule类是这样的:
public class PriorityRule
{
public PriorityRule(RecipientSource recipientSource)
{
RuleType = recipientSource.Name;
Priority = recipientSource.Priority;
}
public PriorityRule() { }
public string RuleType { get; set; }
public int Priority { get; set; }
public RecipientSource RecipientSource { get; set; }
}
.XAML文件是这样的:
<DataGrid
x:Name="PriorityRulesGrid"
HorizontalAlignment="Stretch"
VerticalAlignment="Top"
AutoGenerateColumns="false"
CanUserAddRows="False"
GridLinesVisibility="All"
HorizontalScrollBarVisibility="Auto"
IsReadOnly="False"
RowHeight="32"
VerticalScrollBarVisibility="Auto"
HorizontalContentAlignment="Stretch"
ItemsSource="{Binding PriorityRuleList}"
ColumnWidth="*" >
<DataGrid.Columns>
<DataGridTextColumn Header="{texts:VRI Rule type}"
Binding="{Binding RuleType}"
IsReadOnly="True"
Width="*"/>
<DataGridComboBoxColumn Header="{texts:VRI Priority}"
Width="*"
SelectedValuePath="Priority"
SelectedValueBinding="{Binding Priority}"
DisplayMemberPath="Priority">
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="ItemsSource" Value="{Binding Path=DataContext.PriorityRuleList, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding Path=DataContext.RulePriorityDefaultList, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
</DataGrid.Columns>
</DataGrid>
我的问题是: 单击第二列中的组合框时,我应该如何加载第二个列表。当窗口出现时,第一个列表已加载,因此可以,但是我需要从组合框中选择优先级,所以我无法加载第二个列表(int类型)以便从那里选择?
谢谢!
答案 0 :(得分:0)
好像您正在尝试为DataGridComboBoxColumn.ItemSource
分配两种不同类型的源。保持两个来源正常,但请确保属性Priority
存在。否则,对SelectedValuePath
和DisplayMemberPath
的评估将失败。
创建一个具有属性Priority
的类PriorityX
public class PriorityX
{
public int Priority { get; set; }
}
// assing your default values into this collection
public ObservableCollection<PriorityX> RulePriorityDefaultList { get; set; } = new ObservableCollection<PriorityX>();
在XAML中
<Window.Resources>
<!--load RulePriorityDefaultList as a static resource-->
<CollectionViewSource x:Key="RulePriorityDefaultList" Source="{Binding RulePriorityDefaultList}" />
</Window.Resources>
<Grid>
<DataGrid
x:Name="PriorityRulesGrid"
HorizontalAlignment="Stretch"
VerticalAlignment="Top"
AutoGenerateColumns="false"
CanUserAddRows="False"
GridLinesVisibility="All"
HorizontalScrollBarVisibility="Auto"
IsReadOnly="False"
RowHeight="32"
VerticalScrollBarVisibility="Auto"
HorizontalContentAlignment="Stretch"
ItemsSource="{Binding PriorityRuleList}"
ColumnWidth="*">
<DataGrid.Columns>
<DataGridTextColumn Header="Rule type"
Binding="{Binding RuleType}"
IsReadOnly="True"
Width="*" />
<DataGridComboBoxColumn Header="Priority"
Width="*"
SelectedValuePath="Priority"
SelectedValueBinding="{Binding Priority}"
DisplayMemberPath="Priority"
ItemsSource="{Binding Source={StaticResource RulePriorityDefaultList }}">
</DataGridComboBoxColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
您也可以使用现有的课程。
//assing your default values into this collection
public ObservableCollection<PriorityRule> RulePriorityDefaultList { get; set; } = new ObservableCollection<PriorityRule>();