我有以下简化示例:
<Window x:Class="TemplateBinding.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>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="pack://application:,,,/TemplateBinding;component/PersonTemplate.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<ContentControl ContentTemplate="{StaticResource PersonTemplate}" />
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
附:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<DataTemplate x:Key="PersonTemplate">
<Border Width="100" Height="100" Background="RosyBrown">
<TextBlock Text="{Binding Path=FirstName}" VerticalAlignment="Center" TextAlignment="Center"/>
</Border>
</DataTemplate>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
作为我的DataTemplate在一个单独的ResourceDictionary文件中.
我在我的MainWindow的Construcor中设置了我的DataContext,并通过显示如下的第一个名称来验证它:<ContentControl Grid.Row="1" Content="{Binding FirstName}"/>.
在另一个场景中,我使用DataTemplate与我在DataTemplate中以ListBox完全相同的方式进行绑定,它只是起作用.
我知道DataTemplate除了绑定之外正在工作,因为它正确显示了大小和背景颜色.
我究竟做错了什么?我的DataTemplate中的绑定如何看?
data-binding wpf datatemplate resourcedictionary contentcontrol
我正在尝试创建一个自定义样式面板(StyledStackPanel),它与常规StackPanel相同,具有自定义样式.例如 - 如果样式只是添加下划线,则写入:
<StyledStackPanel>
<!--User content goes here-->
</StyledStackPanel>
Run Code Online (Sandbox Code Playgroud)
我希望收到与我写的相同的外观:
<StackPanel>
<!--User content goes here-->
</StackPanel>
<Border Height="1" BorderBrush="Blue"/>
Run Code Online (Sandbox Code Playgroud)
如果StackPanel是Control,我可以替换该DataTemplate控件.但是,它继承了Panel哪些继承FrameworkElement.
有没有办法改变模板StackPanel?
对WPF来说相当新...
我有一个数据集合,我想绑定到网格面板.每个对象都包含其网格行和列,以及填充在网格位置的内容.我真的很喜欢如何在列表框XAML中创建数据模板来创建一个几乎没有任何代码的UI.有没有办法为网格面板元素创建数据模板,并将面板绑定到数据集合?