msf*_*boy 7 wpf datatemplate contenttemplateselector
我已经制作了一个示例演示VS 2010 RC示例项目,因为在我的生产项目中我使用MVVM时遇到了同样的错误.
在我的示例演示项目中,我只使用Code-behind而没有第三方依赖,因此您可以在此处下载演示项目并自行运行:http://www.sendspace.com/file/mwx7wv
现在问题:当我点击女孩/男孩按钮时,它应该切换datatemplate,不是吗?
我错了什么?
好的,我在这里也提供了一个代码片段:
代码隐藏在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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
:)
我喜欢Neil的解决方案(通过您提供的链接在Josh的帖子中找到):
<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>
Run Code Online (Sandbox Code Playgroud)
编辑:我实际上无法使上面的代码工作,但这使用一种风格:
<DataTrigger Binding="{Binding Path=SourceSystem.SourceSystemName}"
Value="mysite.com">
<Setter Property="ContentControl.ContentTemplate"
Value="{StaticResource mysiteToolbar}" />
Run Code Online (Sandbox Code Playgroud)
<DataTrigger Binding="{Binding Path=SourceSystem.SourceSystemName}"
Value="mysite2.com">
<Setter Property="ContentControl.ContentTemplate"
Value="{StaticResource mysiteToolbar2}" />
Run Code Online (Sandbox Code Playgroud)
</Style.Triggers>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9578 次 |
| 最近记录: |