首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用FrameworkElementFactory在WPF中创建网格

使用FrameworkElementFactory在WPF中创建网格
EN

Stack Overflow用户
提问于 2017-07-11 13:58:35
回答 1查看 1.9K关注 0票数 2

简而言之,我正在把静态XAML描述UI变成一个动态创建的UI。下面是我想在XAML中复制的c#代码:

代码语言:javascript
复制
<GridViewColumn.HeaderTemplate>
    <DataTemplate>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <CheckBox Grid.Column="0"
                      Margin="4,0,5,0"
                      VerticalAlignment="Center"
                      IsChecked="{Binding Path=DataContext.AreAllSelected,
                                          RelativeSource={RelativeSource AncestorType=ListView},
                                          Mode=OneWay}"
                      Command="{Binding Path=DataContext.ChangeAllSourcesSelection,
                                        RelativeSource={RelativeSource AncestorType=ListView}}" />
            <TextBlock Grid.Column="1"
                       VerticalAlignment="Center"
                       UI:DisplayedObjectProperties.DisplayedObject="{Binding}" />
        </Grid>
    </DataTemplate>
</GridViewColumn.HeaderTemplate>

以下是我想出的:

代码语言:javascript
复制
GridViewColumn col = new GridViewColumn();

DataTemplate template = new DataTemplate();

FrameworkElementFactory gridFactory = new FrameworkElementFactory(typeof(Grid));
FrameworkElementFactory col1 = new FrameworkElementFactory(typeof(ColumnDefinition));
FrameworkElementFactory col2 = new FrameworkElementFactory(typeof(ColumnDefinition));

col1.SetValue(ColumnDefinition.WidthProperty, new GridLength(1, GridUnitType.Auto));
col2.SetValue(ColumnDefinition.WidthProperty, new GridLength(1, GridUnitType.Star));
gridFactory.AppendChild(col1);
gridFactory.AppendChild(col2);

FrameworkElementFactory boxFactory = new FrameworkElementFactory(typeof(CheckBox));
boxFactory.SetValue(Grid.ColumnProperty, 1);
boxFactory.SetValue(FrameworkElement.VerticalAlignmentProperty, VerticalAlignment.Center);
FrameworkElementFactory tBlockFactory = new FrameworkElementFactory(typeof(TextBlock));
tBlockFactory.SetValue(Grid.ColumnProperty, 2);
tBlockFactory.SetValue(FrameworkElement.VerticalAlignmentProperty, VerticalAlignment.Center);
tBlockFactory.SetValue(TextBlock.TextProperty, LanguageManager.Parse(columnVM.ColumnName));

gridFactory.AppendChild(boxFactory);
gridFactory.AppendChild(tBlockFactory);
template.VisualTree = gridFactory;

col.HeaderTemplate = template;

然而,这并没有给我想要的效果,因为CheckBox和TextBlock似乎都在同一列中。如何将每个列分配给各自的列?如你所见,我做了一些奇怪的附录,因为我不知道自己在做什么。

以下是我想要的结果:

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-11 14:01:47

应该将Grid.Column附加的TextBlock属性设置为1:

代码语言:javascript
复制
tBlockFactory.SetValue(Grid.ColumnProperty, 1);

没有理由为boxFactory设置属性,因为它的默认值应为0。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45036667

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档