我有ListView
,其中每个元素主要由2列DataGrid
组成。
<ListView Name="SelectedWhereItemListView"
ItemsSource="{Binding AddedWhereItems}"
VerticalContentAlignment="Top">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding TableName}" />
<TextBlock Text="{Binding ColumnName}" />
<DataGrid SelectionMode="Single" AutoGenerateColumns="False" ItemsSource="{Binding WhereFieldCondition}">
<DataGrid.Columns>
<DataGridComboBoxColumn Width="Auto" Header="{lex:Loc Key=Operator}" SelectedValueBinding="{Binding Operator}">
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="ItemsSource" Value="{Binding Path=DataContext.WhereFieldConditions[DataType], RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="ItemsSource" Value="{Binding Path=DataContext.WhereFieldConditions[DataType], RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
<DataGridTextColumn Width="Auto" Header="{lex:Loc Key=Value}" Binding="{Binding Value}" />
</DataGrid.Columns>
</DataGrid>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
我使用DataGrid
为ListView
元素指定1..n运算符/值对。
在当前图形结果下方,只是为了有一个更具体的想法
ListView
的每个元素都是最终生成的查询的where子句。每个块都有一个数据类型(int,varchar等)。基于ListView元素数据类型,我想填充数据网格的DataGridComboBoxColumn
运算符。
我在ViewModel中创建了这个属性
public Dictionary<string, ObservableCollection<string>> WhereFieldConditions
{
get
{
if (_whereFieldconditions == null)
{
_whereFieldconditions = new Dictionary<string, ObservableCollection<string>>();
_whereFieldconditions.Add("int", new ObservableCollection<string>(new string[] { "<", ">", "=", "!=" }.ToList<string>()));
_whereFieldconditions.Add("decimal", new ObservableCollection<string>(new string[] { "<", ">", "=", "!=" }.ToList<string>()));
_whereFieldconditions.Add("nvarchar", new ObservableCollection<string>(new string[] { "=", "like" }.ToList<string>()));
_whereFieldconditions.Add("varchar", new ObservableCollection<string>(new string[] { "=", "like" }.ToList<string>()));
_whereFieldconditions.Add("char", new ObservableCollection<string>(new string[] { "=", "like" }.ToList<string>()));
_whereFieldconditions.Add("datetime", new ObservableCollection<string>(new string[] { "<", ">", "=" }.ToList<string>()));
}
return _whereFieldconditions;
}
}
不幸的是(对我:))我获得了以下异常
System.Windows.Data Error: 17 : Cannot get 'Item[]' value (type 'ObservableCollection`1') from 'WhereFieldConditions' (type 'Dictionary`2'). BindingExpression:Path=DataContext.WhereFieldConditions[.DataType]; DataItem='StockageQueryEditorView' (Name=''); target element is 'TextBlockComboBox' (Name=''); target property is 'ItemsSource' (type 'IEnumerable') TargetInvocationException:'System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
at System.ThrowHelper.ThrowKeyNotFoundException()
at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at MS.Internal.Data.PropertyPathWorker.GetValue(Object item, Int32 level)
at MS.Internal.Data.PropertyPathWorker.RawValue(Int32 k)'
但我目前无法使用ListView项中包含的对象的DataType映射DataGridComboBoxColumn
。我不知道如何修改这个绑定,以使其正常工作,如果这是正确的方法,也不要老实说:
Binding Path=DataContext.WhereFieldConditions[DataType], RelativeSource={RelativeSource AncestorType={x:Type Window}
答案 0 :(得分:1)
但我目前无法使用ListView项中包含的对象的DataType映射DataGridComboBoxColumn
您无法在XAML中执行此操作。 XAML是标记语言,无法使用ItemsSource
解析ListView
[DataType]
中基础对象的类型。这不受支持。
您可以使用multi value converter获取Dictionary<string, ObservableCollection<string>>
和数据对象,并根据对象的类型返回正确的ObservableCollection<string>
:
<Style TargetType="{x:Type ComboBox}">
<Setter Property="ItemsSource">
<Setter.Value>
<MultiBinding Converter="{StaticResource conv}">
<Binding Path="DataContext.WhereFieldConditions" RelativeSource="{RelativeSource AncestorType={x:Type Window}}" />
<Binding Path="DataContext" RelativeSource="{RelativeSource AncestorType=ListViewItem}" />
</MultiBinding>
</Setter.Value>
</Setter>
</Style>