gpt4 book ai didi

wpf - 仅调用ContentTemplateSelector一次,以显示始终相同的数据模板

转载 作者:行者123 更新时间:2023-12-03 18:15:21 25 4
gpt4 key购买 nike

我制作了一个VS 2010 RC样本示例演示项目,因为在我的生产项目中,使用MVVM时遇到相同的错误。

在我的示例演示项目中,我仅使用无第三方依赖的Code-behind,因此您可以在此处下载演示项目并自己运行:http://www.sendspace.com/file/mwx7wv

现在解决问题:当我单击女孩/男孩按钮时,应该切换数据模板,不是吗?

我怎么了

好的,我也在这里提供了一个代码段:

MainWindow.cs背后的代码:

namespace ContentTemplateSelectorDemo
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
Person person;

public MainWindow()
{
InitializeComponent();

person = new Person(){ Gender = "xxx"};
person.IsBoy = true;

ContentGrid.DataContext = person;
}

private void btnBoys_Click(object sender, RoutedEventArgs e)
{
person.IsBoy = true;
person.IsGirl = false;
this.ContentGrid.DataContext = person;
}

private void btnGirls_Click(object sender, RoutedEventArgs e)
{
person.IsGirl = true;
person.IsBoy = false;
this.ContentGrid.DataContext = person;
}
}
}

XAML MainWindow.xaml:
<Window x:Class="ContentTemplateSelectorDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ContentTemplateSelectorDemo"
Title="MainWindow" Height="350" Width="525">

<Window.Resources>
<DataTemplate x:Key="girlsViewTemplate">
<local:UserControl1 />
</DataTemplate>

<DataTemplate x:Key="boysViewTemplate" >
<local:UserControl2 />
</DataTemplate>

<local:PersonDataTemplateSelector x:Key="PersonSelector" />
</Window.Resources>

<Grid x:Name="ContentGrid" >
<StackPanel>
<Button Name="btnGirls" Click="btnGirls_Click">Switch Girls</Button>
<Button Name="btnBoys" Click="btnBoys_Click">Switch Boys</Button>
<ContentControl Content="{Binding}" ContentTemplateSelector="{StaticResource ResourceKey=PersonSelector}" />
</StackPanel>
</Grid>
</Window>

DataTemplateSelector类:
public class PersonDataTemplateSelector : DataTemplateSelector
{
public override DataTemplate SelectTemplate(object item,DependencyObject container)
{
if (item is Person)
{
Person person = item as Person;

Window window = Application.Current.MainWindow;

if (System.ComponentModel.DesignerProperties.GetIsInDesignMode( window))
return null;

if (person.IsBoy)
return window.FindResource("boysViewTemplate") as DataTemplate;
if (person.IsGirl)
return window.FindResource("girlsViewTemplate") as DataTemplate;

}
return null;
}
}

:)

最佳答案

我喜欢Neil的解决方案(可通过Josh's postthe link you provided上找到):

<DataTemplate DataType="{x:Type local:MyType}">
<ContentPresenter Content="{Binding}" Name="cp" />
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=IsRunning}" Value="True">
<Setter TargetName="cp" Property="ContentTemplate" Value="{StaticResource StopTemplate}" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=IsRunning}" Value="False">
<Setter TargetName="cp" Property="ContentTemplate" Value="{StaticResource StartTemplate}" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>

编辑:我实际上无法使上面的代码正常工作,但是使用一种样式可以工作:
<ContentControl DockPanel.Dock="Bottom" >
<ContentControl.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=SourceSystem.SourceSystemName}" Value="mysite.com">
<Setter Property="ContentControl.ContentTemplate" Value="{StaticResource mysiteToolbar}" />
</DataTrigger>

<DataTrigger Binding="{Binding Path=SourceSystem.SourceSystemName}" Value="mysite2.com">
<Setter Property="ContentControl.ContentTemplate" Value="{StaticResource mysiteToolbar2}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>

关于wpf - 仅调用ContentTemplateSelector一次,以显示始终相同的数据模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2565859/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com