WPF中TreeView中的DataGrid

fjnneemd  于 2022-12-14  发布在  其他
关注(0)|答案(3)|浏览(386)

我试图绑定到DataGrid中的StringValue对象集合,但每个项都显示在单独的行中。我希望使用分栏显示,其中56547765656等值显示在同一行中。以下是我当前的代码:

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; }
   }

我想达到的结果是这样的:

我有一个解决方法,但它不支持多重选择。下面是此解决方法的代码:

<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>

如何使用DataGrid实现所需的柱形显示并保持对多选的支持?

pwuypxnk

pwuypxnk1#

而不是DataGrid(它可以进行透视,但会有问题),使用ListBox和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的绑定

djmepvbi

djmepvbi2#

我建议不要使用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));
    }
}

如下所示:

使用列表应该允许您所寻找的方式进行多重选择。

x6h2sr28

x6h2sr283#

建立您要用于数据系结的新类别。
等级

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>

相关问题