我有這個ListView
,其中主要是每個元素由2列DataGrid
組成。將DataGridComboBoxColumn綁定到詞典
<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..1運營商/值對。
低於當前圖形結果,只是有一個更具體的概念
的ListView
的每個元素是一個其中的最終生成的查詢的子句。每個塊都有一個數據類型(int,varchar等)。基於ListView元素數據類型,我想填充數據網格的DataGridComboBoxColumn
運算符。
我在視圖模型創建這個屬性
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)'
但我目前還沒有能夠映射DataGridComboBoxColumn
使用包含在ListView項目中的對象的DataType。我不知道如何修改此約束,以使工作和誠實無論它是否以正確的方式去:
Binding Path=DataContext.WhereFieldConditions[DataType], RelativeSource={RelativeSource AncestorType={x:Type Window}
感謝@ MM8聽起來不錯。我會試一試,並在幾個小時內回覆給你。再次感謝 – gipinani
我正在通過您的解決方案,無論如何只是一個澄清,以避免誤解。 [DataType]只是綁定類的字符串屬性,我並不想要推導出對象類型。 – gipinani
沒有區別我害怕:) – mm8