gpt4 book ai didi

wpf - 如何使用ItemsControl分配

转载 作者:行者123 更新时间:2023-12-03 10:49:30 25 4
gpt4 key购买 nike

我在控件中有一个数据网格(here:DataGridSelectorControl),我想使用ItemTemplate创建它的库伦。实际上我想做这样的事情:

  <DataGrid  x:Name="grd" ItemsSource="{Binding Source={StaticResource theSource}}" AutoGenerateColumns="False"  >
<DataGrid.Columns>
<ItemsControl ItemsSource="{Binding DataGridColumnItems}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</DataGrid.Columns>
</DataGrid>

我想知道有什么方法可以为DataGrid.Columns使用ItemsControl吗?
我想要这样做是因为我想像这样使用此控件:(我想在创建此控件的实例时定义DataGridTextColumns)
<control:DataGridSelectorControl Grid.Row="1">
<control:DataGridSelectorControl.DataGridColumnItems>
<DataGridTextColumn Header="نام" Width="20"></DataGridTextColumn>
<DataGridTextColumn Header="ParentID" Width="20"></DataGridTextColumn>
</control:DataGridSelectorControl.DataGridColumnItems>
</control:DataGridSelectorControl>

和DataGridColumnItems是一个棘手的问题:
  public static readonly DependencyProperty DataGridTextColumnItemsProperty = DependencyProperty.Register("DataTextColumnItems", typeof(DataGridColumn), typeof(DataGridSelectorControl));
public DataGridColumn DataGridColumnItems
{
get
{
return (DataGridColumn)GetValue(DataGridTextColumnItemsProperty);
}
set
{
SetValue(DataGridTextColumnItemsProperty, value);
}
}

最佳答案

DataGrid.Columns的类型为ObservableCollection,请尝试如下操作:

  public static readonly DependencyProperty DataGridTextColumnItemsProperty=DependencyProperty.Register("DataTextColumnItems", typeof(ObservableCollection<DataGridColumn>), typeof(DataGridSelectorControl)))); 
public ObservableCollection<DataGridColumn> DataGridColumnItems
{
get
{
return (ObservableCollection<DataGridColumn>)GetValue(DataGridTextColumnItemsProperty);
}
set
{
SetValue(DataGridTextColumnItemsProperty, value);
}
}

<DataGrid x:Name="grd" ItemsSource="{Binding Source={StaticResource theSource}}" AutoGenerateColumns="False" Columns={Binding DataGridTextColumnItems, RelativeSource={RelativeSource AncestorType=DataGridSelectorControl}}>
</DataGrid>

关于wpf - 如何使用ItemsControl分配<DataGrid.Columns>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10188062/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com