嵌套的 xaml 元素 - 在代码中重写它

发布于 2024-10-21 13:15:21 字数 751 浏览 0 评论 0原文

我需要从代码创建一个 DataGridColumn。 XAML 等效项是:

    <data:DataGridTemplateColumn Header="Name" Width="100">
        <data:DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}" TextTrimming="WordEllipsis"></TextBlock>
            </DataTemplate>
        </data:DataGridTemplateColumn.CellTemplate>
    </data:DataGridTemplateColumn>

我是这样开始的:

 DataGridTemplateColumn column = new DataGridTemplateColumn
   {
      Header = "Name",
      Width = 100,
   };

TextBlock inside =  new TextBlock {TextTrimming = TextTrimming.CharacterEllipsis};

但我不知道如何“合并”这样的谜题。 XAML中有嵌套元素,如何从代码中实现这一点?

I need to create a DataGridColumn from code.
The XAML equivalent would be:

    <data:DataGridTemplateColumn Header="Name" Width="100">
        <data:DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}" TextTrimming="WordEllipsis"></TextBlock>
            </DataTemplate>
        </data:DataGridTemplateColumn.CellTemplate>
    </data:DataGridTemplateColumn>

I've started like that:

 DataGridTemplateColumn column = new DataGridTemplateColumn
   {
      Header = "Name",
      Width = 100,
   };

TextBlock inside =  new TextBlock {TextTrimming = TextTrimming.CharacterEllipsis};

But I don't know how to 'merge' such puzzles. There are nested elements in XAML, how to achieve this from code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扛刀软妹 2024-10-28 13:15:21

执行此操作的一个好方法是将整个 XAML 代码段打包到一个字符串中,并对其调用 XamlReader.Load() 或 XamlReader.Parse()。这种方法的额外功能是它也可以在 Silverlight 中工作(需要一些调整),在这种情况下您无法在代码中构建 DataTemplate

A good way to do this is to pack the entire XAML snippet into a string and call XamlReader.Load() or XamlReader.Parse() on it. The bonus feature of this approach is that it'll work in Silverlight as well (with some fiddling), where you can't build DataTemplates in code.

驱逐舰岛风号 2024-10-28 13:15:21

差不多就这样,将您的代码更改为这样,它应该可以工作:

DataGridTemplateColumn column = new DataGridTemplateColumn
   {
      Header = "Name",
      Width = 100,
   };
FrameworkElementFactory ftb = new FrameworkElementFactory(typeof(TextBlock));
Binding b = new Binding("Name");
ftb.SetValue(TextBlock.Text, b);
ftb.SetValue(TextBlock.TextTrimming, TextTrimming.CharacterEllipsis);    
DataTemplate ct = new DataTemplate();
ct.VisualTree = ftb;
column.CellTemplate = ct;

除上述之外的另一种方法是在资源中的 XAML 中定义数据模板,然后在代码中动态加载它:

XAML:

<Window.Resources>
    <DataTemplate x:Key="myCellTemplate">
        <TextBlock Text="{Binding Name}" TextTrimming="WordEllipsis" />
    </DataTemplate>
</Window.Resources>

代码:

DataGridTemplateColumn column = new DataGridTemplateColumn
   {
      Header = "Name",
      Width = 100,
   };
column.CellTemplate = this.FindResource("myCellTemplate") as DataTemplate;

Almost there, change your code to this and it should work:

DataGridTemplateColumn column = new DataGridTemplateColumn
   {
      Header = "Name",
      Width = 100,
   };
FrameworkElementFactory ftb = new FrameworkElementFactory(typeof(TextBlock));
Binding b = new Binding("Name");
ftb.SetValue(TextBlock.Text, b);
ftb.SetValue(TextBlock.TextTrimming, TextTrimming.CharacterEllipsis);    
DataTemplate ct = new DataTemplate();
ct.VisualTree = ftb;
column.CellTemplate = ct;

Another method besides the above is to define your datatemplate in XAML within your resources then dynamically load it in the code:

XAML:

<Window.Resources>
    <DataTemplate x:Key="myCellTemplate">
        <TextBlock Text="{Binding Name}" TextTrimming="WordEllipsis" />
    </DataTemplate>
</Window.Resources>

Code:

DataGridTemplateColumn column = new DataGridTemplateColumn
   {
      Header = "Name",
      Width = 100,
   };
column.CellTemplate = this.FindResource("myCellTemplate") as DataTemplate;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文