大家好,我正试图在我的dataGrid中更改列的模板,但是我找不到在XAML中这样做的方法。我正试着用这种方式来做
<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>但是一个错误告诉我,The property VisualTree cannot be set as a property element on template. Only Triggers and Storyboards are allowed as property elements知道根据另一个控件改变DataGridCell中模板的不同方法吗?
发布于 2012-07-17 03:59:12
您不能在模板中更改模板,这不是它的工作方式。
有许多方法可以做到这一点。取决于应用程序的配置方式。最常用的方法是
绑定到property/collection
例如,您的应用程序中有几个模型
public sealed class Foo
{
public string Text {get;set;}
}
public sealed class Bar
{
public bool Checked {get;set;}
}您的应用程序公开一个包含一个或多个此类实例的属性
public partial class MainWindow : Window
{
//INotifyPropertyChanged/DependencyObject stuff left out!
public object FooOrBar {get;set;}
//snip
}在XAML中,有一个扩展ItemsControl或ContentControl或类似类型的UIElement类型,可以绑定到该属性。
<Window
x:Name="root"
xmlns:t="clr-namespace:MyApplicationWhereFooAndBarLive"
SkipAllThatXmlnsDefinitionNonsenseForSpaceSavingsInThisExample="true"/>
<!-- see Resources below -->
<ConentControl Content="{Binding FooOrBar, ElementName=root}" />
</Window>最后,在应用程序的资源中为每个类型定义DataTemplates
<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选择的过程如下所示:
FooOrBar = new Foo();
LoadContent()方法在其中加载视觉树。
Foo)
ItemsControl的情况大致相同,只是添加了一个中介(即ListBox使用ListBoxItem作为中介,而LBI是一个ContentControl)。
https://stackoverflow.com/questions/11511285
复制相似问题