Dar*_*Zon 8 c# wpf xaml templates datatemplate
想象一下,在一个数据模板中,我有一个textBox和另一个数据模板,我有两个文本框.
根据这个,在视图中有一个复选框,并显示每个模板..这可能吗?
对不起,如果我的问题是如此怀疑,我已经调查了但是我没有发现.
我这样做了,我知道这没用,但仅用于测试.
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<DataTemplate DataType="{x:Type ContentControl}" x:Key="T1">
<StackPanel>
<TextBox Height="20" />
</StackPanel>
</DataTemplate>
<DataTemplate DataType="{x:Type ContentControl}" x:Key="T2">
<StackPanel>
<TextBox Height="20" />
<TextBox Height="20" />
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<ContentControl Template="{StaticResource T1}" />
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
小智 6
您可以在较低级别指定一个模板.就像是:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<DataTemplate DataType="{x:Type ContentControl}" x:Key="T1">
<StackPanel>
<TextBox Height="20" />
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<ContentControl Template="{StaticResource T1}">
<ContentControl.Resources>
<DataTemplate DataType="{x:Type ContentControl}" x:Key="T2">
<StackPanel>
<TextBox Height="20" />
<TextBox Height="20" />
</StackPanel>
</DataTemplate>
<ContentControl.Resources>
</ContentControl>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
您的设计应该包括一个模板选择器......
DataTemplates 是 WPF 中极其强大的部分,通过使用它们,您可以抽象出各种显示代码。然而,有时它们会出现不足 - 最初当我学习 WPF 时,我对此感到失望。例如,您只能在项目控件上设置一个 DataTemplate,虽然这很有意义,但感觉很有限。如果我想根据项目的内容使用不同的模板怎么办?我是否必须将所有逻辑构建到单个数据模板中?
来源: 打开代码
这是 WPF 对您问题的回答,应该会产生您想要的行为。本教程有一些清晰的示例来展示该技术......
| 归档时间: |
|
| 查看次数: |
20062 次 |
| 最近记录: |