从代码中找到ControlTemplate

发布于 2024-08-09 03:05:50 字数 1387 浏览 1 评论 0原文

我有一个包含以下代码的 xaml 文件:

<GridViewColumn x:Name="lvCol3"
                Header="Quantità"
                Width="120">
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <Control x:Name="host">
                <Control.Template>
                    <ControlTemplate>
                        <TextBlock Text="{Binding Path=Entity.Quantita}" />
                    </ControlTemplate>
                </Control.Template>
            </Control>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding Path=IsSelected,
                                       RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}"
                             Value="True">
                    <Setter TargetName="host" Property="Template">
                        <Setter.Value>
                            <ControlTemplate x:Name="myControlTemplate" />
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

我将从后面的代码管理 myControlTemplate 以便分配不同的 UI 对象。

我尝试使用 FindResource,但它不起作用。我该怎么做?

I have a xaml file with this code:

<GridViewColumn x:Name="lvCol3"
                Header="Quantità"
                Width="120">
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <Control x:Name="host">
                <Control.Template>
                    <ControlTemplate>
                        <TextBlock Text="{Binding Path=Entity.Quantita}" />
                    </ControlTemplate>
                </Control.Template>
            </Control>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding Path=IsSelected,
                                       RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}"
                             Value="True">
                    <Setter TargetName="host" Property="Template">
                        <Setter.Value>
                            <ControlTemplate x:Name="myControlTemplate" />
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

I would manage myControlTemplate from code behind in order to assign different UI object.

I try to use FindResource but it doesn't work. How can I do this?

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

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

发布评论

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

评论(1

不寐倦长更 2024-08-16 03:05:50

也许最简单的方法是将 ControlTemplate 提取到资源中:然后您可以使用 FindResource。

像这样的事情:

<UserControl>
  <UserControl.Resources>
    <ControlTemplate x:Key="MyControlTemplate">
         <TextBlock
            Text="{Binding Path=Entity.Quantita}"/>
    </ControlTemplate>
  </UserControl.Resources>

  ...

  <GridViewColumn 
     x:Name="lvCol3"
     Header="Quantità"
     Width="120">
        <GridViewColumn.CellTemplate>
            <DataTemplate>
               <Control x:Name="host" Template="{StaticResouce MyControlTemplate}">   
               </Control>
        </GridViewColumn.CellTemplate>
   </GridViewColumn>

  ...
</UserControl>

然后在您的 UserControl (或任何根元素)的代码中,您可以执行以下操作

var resource = FindResource("MyControlTemplate") as ControlTemplate;

Probably the easiest way is to extract your ControlTemplate to a resource: then you can use FindResource.

Something like this:

<UserControl>
  <UserControl.Resources>
    <ControlTemplate x:Key="MyControlTemplate">
         <TextBlock
            Text="{Binding Path=Entity.Quantita}"/>
    </ControlTemplate>
  </UserControl.Resources>

  ...

  <GridViewColumn 
     x:Name="lvCol3"
     Header="Quantità"
     Width="120">
        <GridViewColumn.CellTemplate>
            <DataTemplate>
               <Control x:Name="host" Template="{StaticResouce MyControlTemplate}">   
               </Control>
        </GridViewColumn.CellTemplate>
   </GridViewColumn>

  ...
</UserControl>

Then in the code for your UserControl (or whatever is the root element) you can do

var resource = FindResource("MyControlTemplate") as ControlTemplate;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文