2017-03-01 242 views
2

我試圖綁定到下面DataGridStringValue對象的集合。但是每個項目都顯示在單獨的行中。WPF中的TreeView中的DataGrid

我希望它有一個柱狀顯示。例如,值565477和65656應該顯示在一行中。

enter image description here

XAML:

<TreeView> 
    <TreeViewItem Header="Warnings"> 
     <TreeView ItemsSource="{Binding WarningCategories}"> 
      <TreeView.ItemTemplate> 
       <HierarchicalDataTemplate ItemsSource="{Binding Path=Warnings}"> 
        <TextBlock Text="{Binding Path=Category}" /> 
        <HierarchicalDataTemplate.ItemTemplate> 
         <DataTemplate> 
          <DataGrid ItemsSource="{Binding Fields}" AutoGenerateColumns="True" HeadersVisibility="None"> 
          </DataGrid>         
         </DataTemplate> 
        </HierarchicalDataTemplate.ItemTemplate> 
       </HierarchicalDataTemplate> 
      </TreeView.ItemTemplate> 
     </TreeView> 
    </TreeViewItem> 
</TreeView> 

代碼隱藏:

public class ViewModel : INotifyPropertyChanged 
    { 
     public event PropertyChangedEventHandler PropertyChanged; 

     public ObservableCollection<WarningCategory> WarningCategories { get; set; } 


     public ViewModel() 
     { 
     WarningCategories = new ObservableCollection<WarningCategory>(); 

     ObservableCollection<Warning> warnings = new ObservableCollection<Warning> 
     { 
      new Warning(new StringValue("565477"), new StringValue("65656")), 
      new Warning(new StringValue("767455"), new StringValue("75642")), 
     }; 


     WarningCategories.Add(new WarningCategory("Warning One", warnings)); 



     warnings = new ObservableCollection<Warning> 
     { 
      new Warning(new StringValue("565477"), new StringValue("65656")), 
      new Warning(new StringValue("767455"), new StringValue("75642")), 
     }; 



     WarningCategories.Add(new WarningCategory("Warning Two", warnings)); 
     } 



     [NotifyPropertyChangedInvocator] 
     protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) 
     { 
     var handler = PropertyChanged; 
     if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

    public class WarningCategory 
    { 
     public string Category { get; set; } 

     private ObservableCollection<Warning> m_warnings; 

     public ObservableCollection<Warning> Warnings 

     { 
     get 
     { 
      return m_warnings ?? (m_warnings = new ObservableCollection<Warning>()); 
     } 
     } 


     public WarningCategory(string category, ObservableCollection<Warning> animals) 

     { 
     Category = category; 

     m_warnings = animals; 
     } 
    } 

    public class Warning 

    { 
     public ObservableCollection<StringValue> Fields { get; set; } 

     public Warning(params StringValue[] fields) 

     { 
     Fields = new ObservableCollection<StringValue>(fields); 
     } 
    } 

    public class StringValue 
    { 
     public StringValue(string s) 
     { 
     Value = s; 
     } 
     public string Value { get; set; } 
    } 

我想最後的結果是這樣的:

enter image description here

我的解決方法到目前爲止,但遺憾的是它不支持多選!

<ItemsControl ItemsSource="{Binding Fields}"> 
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <StackPanel Orientation="Horizontal"/> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <TextBlock Text="{Binding Value}" Width="150"/> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 
+0

你真的想要在數據網格中有多行嗎?爲什麼你使用數據網格? –

+0

@DanField實際上沒有。但我想能夠選擇整行。我發現DataGrid中的功能 - – Vahid

+0

您可以在文章中添加一些內容,以顯示您期望最終輸出的樣子嗎? –

回答

-1

創建一個您將用於數據綁定的新類。

CLASS

Public class MainClass 
{ 
    public void MainClass(string row,string rowValue) 
    { 
     Row = row; 
     RowValue= rowValue; 
    } 
    public string Row 
    { 
    get; 
    set; 
    } 
    public string RowValue 
    { 
    get; 
    set; 
    } 
} 

視圖模型

 private ObservableCollection<MainClass> _mainClassList = new ObservableCollection<MainClass>(); 
     public ObservableCollection<MainClass> MainClassList 
     { 
      get { return _mainClassList; } 
      set 
      { 
       _mainClassList= value; 
       NotifyPropertyChanged(m => m.MainClassList); 
      } 
     } 
     public void AddItems() 
     { 
     _mainClassList.Add(new MainClass("row1","row1Value")) 
     _mainClassList.Add(new MainClass("row2","row2Value")) 
     MainClassList = _mainClassList; 
     } 

XAML的

<DataGrid CanUserAddRows="True" RowHeight="23" AutoGenerateColumns="False" ItemsSource="{Binding MainClassList}"> 
       <DataGrid.Columns> 
        <DataGridTextColumn Width="70*" Header="Column1" Binding="{Binding Row,Mode=OneWay}"></DataGridTextColumn> 
        <DataGridTextColumn Width="70*" Header="Column2" Binding="{Binding RowValue,Mode=OneWay}"></DataGridTextColumn> 
       </DataGrid.Columns> 
</DataGrid> 
+0

什麼是Column1Value? – Vahid

+0

@Vahid編輯它爲你 –

+0

@Vahid它的工作原理和做你想要它做的,爲什麼downvote? –

1

代替數據網格(其可以樞轉,但它是有問題的)使用列表框具有用於ItemsPanel

水平的StackPanel
<DataTemplate> 
    <ListBox ItemsSource="{Binding Fields}"> 
    <ListBox.ItemsPanel> 
     <ItemsPanelTemplate> 
     <StackPanel Orientation="Horizontal"/> 
     </ItemsPanelTemplate> 
    </ListBox.ItemsPanel> 

    <ListBox.ItemTemplate> 
     <DataTemplate> 
     <TextBlock Text="{Binding Path=Value}"/> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
    </ListBox>  

    <ListBox.ItemContainerStyle> 
     <Style TargetType="ListBoxItem"> 
      <Setter Property="IsSelected" 
        Value="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType=TreeViewItem}}"/> 
     </Style> 
    </ListBox.ItemContainerStyle> 
</DataTemplate> 

,使全行選擇我創建ListBoxItem.IsSelected來TreeViewItem.IsSelected

+0

感謝Ash,我希望能夠選擇整行。 – Vahid

+0

@Vahid,看我的編輯 – ASh

+0

謝謝。它的工作原理,但只有當我點擊一個單元格時,整個行才被選中,懸停時只有一個單元格被選中。 – Vahid

0

我建議對一個DataGrid結合,我也想返工你的模板當前正在使用的方式。這樣的事情是非常接近你想要什麼,我想:

<TreeView> 
    <TreeView.Resources> 
     <HierarchicalDataTemplate DataType="{x:Type local:WarningCategory}" ItemsSource="{Binding Warnings}"> 
      <TextBlock Text="{Binding Path=Category}" /> 
      <HierarchicalDataTemplate.ItemTemplate> 
       <DataTemplate> 
        <ListBox ItemsSource="{Binding}" SelectionMode="Extended"> 
         <TextBlock Text="{Binding FieldsPivoted}" /> 
        </ListBox> 
       </DataTemplate> 
      </HierarchicalDataTemplate.ItemTemplate> 
     </HierarchicalDataTemplate> 
     <!--<DataTemplate DataType="{x:Type local:Warning}"> 
      <TextBlock Text="{Binding FieldsPivoted}" /> 
     </DataTemplate>--> 
    </TreeView.Resources> 

    <TreeViewItem Header="Warnings" ItemsSource="{Binding WarningCategories}" /> 
</TreeView> 

有了這個添加到您的Warning類:

public string FieldsPivoted 
{ 
    get 
    { 
     return string.Join(", ", Fields.Select(x => x.Value)); 
    } 
} 

是這樣的:

enter image description here

使用一個列表應該允許您選擇多種選擇。

+0

丹,我現在使用類似的方法,但不幸的是,這並沒有解決我的問題。請看我編輯: – Vahid

+0

編輯 - 它現在有一個列表框,允許多行選擇。 –

+0

感謝丹,請你上傳圖片。我錯過了一些東西。我現在可以選擇一行。 – Vahid