在 DataTemplate.Triggers 中设置 DataTemplate.VisualTree [英] setting DataTemplate.VisualTree in DataTemplate.Triggers

查看:81
本文介绍了在 DataTemplate.Triggers 中设置 DataTemplate.VisualTree的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在尝试更改 dataGrid 中列的模板,但我找不到在 XAML 中执行此操作的方法.我正在尝试通过这种方式来做到这一点

Hello everybody I am trying to change the template of a column in my dataGrid but I cannot find the way to do it in XAML. I am trying to do it by this way

<DataTemplate>
    <DataTemplate.Triggers>
        <Trigger Property="{Binding ElementName=isComboBox, Path=IsChecked}"
                 Value="True">
            <Setter Property="VisualTree">
                <Setter.Value>
                    <ComboBox ItemsSource="{Binding elementos}"/>                
                </Setter.Value>
            </Setter>
        </Trigger>
    </DataTemplate.Triggers>
</DataTemplate>

但是一个错误告诉我不能将属性 VisualTree 设置为模板上的属性元素.仅允许触发器和情节提要作为属性元素 是否有人知道根据另一个控件更改 DataGridCell 中模板的不同方法?

but an error show me that The property VisualTree cannot be set as a property element on template. Only Triggers and Storyboards are allowed as property elements does any body know a different way to change template in a DataGridCell according another control ?

推荐答案

你不改变模板在模板内,它不是这样工作的.

You don't change the template within the template, that's not how it works.

有很多方法可以做到这一点.取决于您的应用程序的配置方式.最常用的方法是

There are a number of ways to do this. Depends on how your application is configured. The most common method is

  1. ContentControl 或 ItemsControl 绑定到属性/集合
  2. 集合包含一个或多个不同类型的实例
  3. 您在应用程序的资源中定义 DataTemplates,其具有与您的实例类型匹配的 DataType

例如,您的应用程序中有几个模型

For example, you have a few Models in your application

public sealed class Foo
{
    public string Text {get;set;}
}

public sealed class Bar
{
    public bool Checked {get;set;}
}

您的应用程序公开了一个包含一个或多个这些实例的属性

Your application exposes a property that contains one or more of these instances

public partial class MainWindow : Window
{
    //INotifyPropertyChanged/DependencyObject stuff left out!
    public object FooOrBar {get;set;} 

    //snip
}

在您的 XAML 中,您有一个扩展 ItemsControlContentControl 或类似的可以绑定到属性.

In your XAML, you have a UIElement type that extends ItemsControl or ContentControl or similar that can bind to the property.

<Window 
    x:Name="root"
    xmlns:t="clr-namespace:MyApplicationWhereFooAndBarLive"
    SkipAllThatXmlnsDefinitionNonsenseForSpaceSavingsInThisExample="true"/>  
    <!-- see Resources below --> 
    <ConentControl Content="{Binding FooOrBar, ElementName=root}" />
</Window>

最后,在应用程序的资源中为每种类型定义 DataTemplates

Lastly, you define DataTemplates for each of your types within the Resources of your application

<Window.Resources>
    <DataTemplate DataType="{x:Type t:Foo}">
        <TextBox Text="{Binding Text}" />
    </DataTemplate >
    <DataTemplate DataType="{x:Type t:Bar}">
        <CheckBox Checked="{Binding Checked}" />
    </DataTemplate >
</Window.Resources>

DataTemplate 选择的过程是这样的:

The process of DataTemplate selection goes like this:

  1. 某处有人设置(我们将在本例中说)FooOrBar = new Foo();
  2. ContentControl 的内容绑定更新(通过 INPC 或 DependencyProperty)
  3. ContentControl 寻找它的 DataTemplateSelector, 发现没有配置并使用默认实现.
  4. 默认的 DataTemplateSelector 获取绑定到 Content 属性的对象的类型.
  5. DataTemplateSelector(本质上)查找逻辑树作为 DataTemplate 并且具有与步骤 4 中标识的实例类型匹配的类型键的资源.
  6. 找到 DataTemplate,并将其传递给 ConentControl
  7. ContentControl 通过 <代码>LoadContent() 方法.
  8. ContentControl 将这个可视化树的根的 DataContext 设置为 Content 属性中的值(在我们的例子中,是 Foo 的新实例)
  9. 此可视化树 (IIRC) 添加为 ConentControl 的子项,现在在 UI 中可见.
  1. Someone somewhere sets (we'll say in this example) FooOrBar = new Foo();
  2. The ContentControl's Content binding updates (via INPC or DependencyProperty)
  3. The ContentControl looks for its DataTemplateSelector, finds none configured and uses the default implementation.
  4. The default DataTemplateSelector gets the type of the object bound to the Content property.
  5. The DataTemplateSelector (essentially) looks up the logical tree for a Resource that is a DataTemplate and that has a key of a type that matches the instance type identified in step 4.
  6. The DataTemplate is found, and is passed to the ConentControl
  7. The ContentControl Loads the visual tree within via the LoadContent() method.
  8. The ContentControl sets the DataContext of the root of this visual tree to the value within the Content property (in our case, the new instance of Foo)
  9. This visual tree is (IIRC) added as a child of the ConentControl and is now visible within the UI.

ItemsControl 大致相同,只是添加了一个中介(即 ListBox 使用 ListBoxItem 作为中介,而 LBI 是一个 ContentControl).

Roughly the same thing goes for an ItemsControl, except an intermediary is added (i.e., ListBox uses the ListBoxItem as an intermediary, and LBI is a ContentControl).

这篇关于在 DataTemplate.Triggers 中设置 DataTemplate.VisualTree的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆