2017-07-12 75 views
0

我有這個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運營商/值對。

低於當前圖形結果,只是有一個更具體的概念

enter image description here

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} 

回答

1

但我目前無法使用數據類型映射DataGridComboBoxColumn ListView項目中包含的對象

您不能在XAML中執行此操作。 XAML是標記語言,並且使用[DataType]將無法​​解析ListViewItemsSource中的基礎對象的類型。這不支持。

你可以使用一個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> 
+1

感謝@ MM8聽起來不錯。我會試一試,並在幾個小時內回覆給你。再次感謝 – gipinani

+0

我正在通過您的解決方案,無論如何只是一個澄清,以避免誤解。 [DataType]只是綁定類的字符串屬性,我並不想要推導出對象類型。 – gipinani

+1

沒有區別我害怕:) – mm8

相關問題