Visual Studio在尝试将DataTemplate分配给WPF窗口时崩溃
当我谈到WPF时,我是一个相对的新手,但我正在尝试为我的Windows创建一个可重用的模板。最终目标是拥有一个模板,该模板可以应用于我的应用程序中的所有对话框窗口,并具有可自定义的内容。最终,我希望模板允许内容的两个不同部分 将来自其他web解决方案的片段拼凑到一起这就是我目前所拥有的: my Application.xaml文件中的Visual Studio在尝试将DataTemplate分配给WPF窗口时崩溃,wpf,visual-studio-2010,xaml,datatemplate,Wpf,Visual Studio 2010,Xaml,Datatemplate,当我谈到WPF时,我是一个相对的新手,但我正在尝试为我的Windows创建一个可重用的模板。最终目标是拥有一个模板,该模板可以应用于我的应用程序中的所有对话框窗口,并具有可自定义的内容。最终,我希望模板允许内容的两个不同部分 将来自其他web解决方案的片段拼凑到一起这就是我目前所拥有的: my Application.xaml文件中的DataTemplate。还是很粗糙,因为我只是想让它工作: <DataTemplate x:Key="myDialogTemplate">
DataTemplate
。还是很粗糙,因为我只是想让它工作:
<DataTemplate x:Key="myDialogTemplate">
<DockPanel>
<Button DockPanel.Dock="Bottom" Content="OK"/>
<Button DockPanel.Dock="Bottom" Content="Cancel"/>
<Border Background="Red" BorderBrush="Black" BorderThickness="1" Padding="5">
<ContentPresenter Content="{Binding ElementName=Elt2}" />
</Border>
<Border BorderBrush="Black" BorderThickness="1" Padding="5">
<ContentPresenter Content="{Binding ElementName=Elt1}" />
</Border>
</DockPanel>
</DataTemplate>
因此,据我所知,ContentPresenters
应该路由到Elt1
和Elt2
按钮,这些按钮位于ItemsControl
中。有时,这实际上会根据需要运行和显示。但有时在ContentPresenter
行上打开Application.xaml文件时出错:
You need to set Binding.Path for a TwoWay Binding using ElementName or RelativeSource on a Content Property.
如果我偶然将
模式
设置为双向以外的其他模式,错误仍然会出现。而且,每次我在VisualStudio中打开特定窗口的xaml文件时,designer都会冻结然后崩溃。不知道为什么。窗口。内容的值是放置在窗口中的第一项。在您的情况下,它是ItemsControl实例。因此,您将为ItemsControl放置一个模板
我觉得你想做你的按钮模板,对吗
<DataTemplate x:Key="myDialogTemplate">
<Border BorderBrush="Black" BorderThickness="1" Padding="5">
<ContentPresenter Content="{Binding}" />
</Border>
</DataTemplate>
<ItemsControl>
<ContentControl x:Name="Elt1">
<Button ContentTemplate="{StaticResource myDialogTemplate}">Stuff</Button>
</ContentControl>
<ContentControl x:Name="Elt2">
<Button ContentTemplate="{StaticResource myDialogTemplate}">Stuff 2</Button>
</ContentControl>
</ItemsControl>
东西
材料2
我希望这对你有更进一步的帮助。这里有几件事是有点。。。嗯,非常规 第一件事:使用
ElementName=xyz
绑定时,需要指定要绑定到的属性,这是路径。您不能绑定到对象本身,即绑定到名为Elt1/2
的ContentControl
s
带有ElementName
的绑定需要指定路径,即:
Content="{Binding ElementName=Elt1, Path=Content}"
或
都是一样的
其次,您对窗口的使用有点混乱。内容模板用于指定如何呈现数据,它是raw
,表示不是UIElements
。说你有课
公共类MyModel
{
公共字符串Hello{get;set;}
公共字符串世界{get;set;}
}
如果您将此实例设置为窗口的内容
,它将显示为
Namespace.MyModel
这是无用的,因此使用模板指定如何呈现数据:
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Hello}" />
<TextBlock Text="{Binding World}" />
</StackPanel>
</DataTemplate>
然后像这样使用它:
<views:Dialog>
<views:Dialog.Content1>
<Button>Stuff</Button>
</views:Dialog.Content1>
<views:Dialog.Content2>
<Button>Stuff 2</Button>
</views:Dialog.Content2>
</views:Dialog>
东西
材料2
这是否回答了您的问题?我认为要求内容演示者具有不同的背景。。。?但也许我弄错了。我试图添加一个更好的解释。我想为Windows创建一个模板,但该模板将有两个不同的内容部分。。。每个部分都可以是任何东西。。。按钮,网格,随便什么。对不起,我想我没解释清楚。我想为所有对话框窗口创建一个可重用的模板,这样我就不必为每个窗口重新声明ok按钮、cancel按钮、边框等。当然,重新键入这些代码片段并不麻烦,但我认为“正确”的方法是以某种方式将它们转储到模板中,然后用于每个对话框窗口。
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Hello}" />
<TextBlock Text="{Binding World}" />
</StackPanel>
</DataTemplate>
<Window x:Class="TestWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="TestWindow" Height="300" Width="300"
ContentTemplate="{StaticResource myDialogTemplate}" >
<DockPanel>
<Button DockPanel.Dock="Bottom" Content="OK"/>
<Button DockPanel.Dock="Bottom" Content="Cancel"/>
<Border Background="Red" BorderBrush="Black" BorderThickness="1" Padding="5">
<ContentControl x:Name="Elt2">
<Button>Stuff</Button>
</ContentControl>
</Border>
<Border BorderBrush="Black" BorderThickness="1" Padding="5">
<ContentControl x:Name="Elt1">
<Button>Stuff</Button>
</ContentControl>
</Border>
</DockPanel>
</Window>
<UserControl x:Class="MyApp.Views.Dialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:views="clr-namespace:MyApp.Views"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<DockPanel>
<Button DockPanel.Dock="Bottom" Content="OK"/>
<Button DockPanel.Dock="Bottom" Content="Cancel"/>
<Border Background="Red" BorderBrush="Black" BorderThickness="1" Padding="5">
<ContentPresenter Content="{Binding Content1, RelativeSource={RelativeSource FindAncestor, AncestorType=views:Dialog}}" />
</Border>
<Border BorderBrush="Black" BorderThickness="1" Padding="5">
<ContentPresenter Content="{Binding Content2, RelativeSource={RelativeSource FindAncestor, AncestorType=views:Dialog}}" />
</Border>
</DockPanel>
</UserControl>
<views:Dialog>
<views:Dialog.Content1>
<Button>Stuff</Button>
</views:Dialog.Content1>
<views:Dialog.Content2>
<Button>Stuff 2</Button>
</views:Dialog.Content2>
</views:Dialog>