Wpf 使用ResourceDictionary时无法使用XAML绑定到类实例的属性
在我的Wpf 使用ResourceDictionary时无法使用XAML绑定到类实例的属性,wpf,xaml,resourcedictionary,Wpf,Xaml,Resourcedictionary,在我的WPF项目中,我使用自定义我的main窗口。当我尝试绑定Person.xaml.cscode-behind类的Person.xaml文件的属性LastName时,出现以下错误,如下所示: 异常:找不到名为“prs”的资源。资源名称区分大小写 备注 该错误似乎是由于下面显示的ResourceDictionary.xaml文件中的引用引起的。并且错误发生在第InitializeComponent()行 如果我在App.xaml文件中引用,则会发生完全相同的错误 但是,如果我不使用Resourc
WPF
项目中,我使用自定义我的main窗口
。当我尝试绑定Person.xaml.cs
code-behind类的Person.xaml
文件的属性LastName
时,出现以下错误,如下所示:
异常:找不到名为“prs”的资源。资源名称区分大小写
备注
ResourceDictionary.xaml
文件中的
引用引起的。并且错误发生在第InitializeComponent()行当Person.xaml.cs类在下面显示的ResourceDictionary.xaml.cs`文件中定义的按钮单击事件中实例化时,Persona.xaml.cs
文件的code>
App.xaml
文件中引用
,则会发生完全相同的错误ResourceDictionary
自定义mainWindow
而在App.xaml文件中引用
,则不会发生错误<Window x:Class="MyProject.Commands.Person"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MyProject">
<Grid>
<Label>Last Name:</Label>
<TextBox Name="txtLastName">
<TextBox.Text>
<Binding Path="LastName" Source="{StaticResource prs}" UpdateSourceTrigger="PropertyChanged">
</Binding>
</TextBox.Text>
</TextBox>
</Grid>
</Window
namespace SciDoxViewer
{
public partial class WindowStyle : ResourceDictionary
{
public WindowStyle()
{
InitializeComponent();
}
//Here: click events for other buttons in above ResourceDictionary.xaml file
...............
...............
private void btnAddPerson_Click(object sender, RoutedEventArgs e)
{
Person person = new Person(); //This line first takes you to `InitializeComponent();` of the constructor of `Person` class where the above error occurs.
if (person.ShowDialog() == true)
{
.....
}
}
}
}
<Window x:Class="MyProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MyProject"
mc:Ignorable="d"
Style="{StaticResource CustomWindowStyle}">
<Grid>
....
</Grid>
</Window>
<Application x:Class="MyProject.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SciDoxViewer.Commands"
xmlns:main="clr-namespace:MyProject"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MyProject;component/WindowStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
ResourceDictionary.xaml:数据源
在第一行中引用
<ResourceDictionary x:Class="MyProject.WindowStyle"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyProject.Commands">
<local:Person x:Key="pers"/>
<Style x:Key="CustomWindowStyle" TargetType="{x:Type Window}">
<Setter Property="WindowChrome.WindowChrome">
<Setter.Value>
<WindowChrome CaptionHeight="30"
CornerRadius="4"
GlassFrameThickness="0"
NonClientFrameEdges="None"
ResizeBorderThickness="5"
UseAeroCaptionButtons="False" />
</Setter.Value>
</Setter>
<Setter Property="BorderBrush" Value="Black" />
<Setter Property="Background" Value="Gray" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Window}">
<Grid>
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="5,30,5,5">
<AdornerDecorator>
<ContentPresenter />
</AdornerDecorator>
</Border>
<DockPanel Height="30"
VerticalAlignment="Top"
LastChildFill="False">
<TextBlock Margin="5,0,0,0"
VerticalAlignment="Center"
DockPanel.Dock="Left"
FontSize="16"
Foreground="White"
Text="{TemplateBinding Title}" />
<Button x:Name="btnClose"
Width="15"
Margin="5"
Click="CloseClick"
Content="X"
DockPanel.Dock="Right"
WindowChrome.IsHitTestVisibleInChrome="True" />
<Button x:Name="btnRestore"
Width="15"
Margin="5"
Click="MaximizeRestoreClick"
Content="#"
DockPanel.Dock="Right"
WindowChrome.IsHitTestVisibleInChrome="True" />
<Button x:Name="btnMinimize"
Width="15"
Margin="5"
VerticalContentAlignment="Bottom"
Click="MinimizeClick"
Content="_"
DockPanel.Dock="Right"
WindowChrome.IsHitTestVisibleInChrome="True" />
<Button x:Name="btnAddPerson"
Width="15"
Margin="5"
VerticalContentAlignment="Bottom"
Click="btnAddPerson_Click"
Content="_"
DockPanel.Dock="Right"
WindowChrome.IsHitTestVisibleInChrome="True" />
</DockPanel>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
更新[日期:2020年11月5日]
WindowStyle.xaml
在main窗口中引用(而不是在Person.xaml
中),如下所示。
主窗口.xaml:
<Window x:Class="MyProject.Commands.Person"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MyProject">
<Grid>
<Label>Last Name:</Label>
<TextBox Name="txtLastName">
<TextBox.Text>
<Binding Path="LastName" Source="{StaticResource prs}" UpdateSourceTrigger="PropertyChanged">
</Binding>
</TextBox.Text>
</TextBox>
</Grid>
</Window
namespace SciDoxViewer
{
public partial class WindowStyle : ResourceDictionary
{
public WindowStyle()
{
InitializeComponent();
}
//Here: click events for other buttons in above ResourceDictionary.xaml file
...............
...............
private void btnAddPerson_Click(object sender, RoutedEventArgs e)
{
Person person = new Person(); //This line first takes you to `InitializeComponent();` of the constructor of `Person` class where the above error occurs.
if (person.ShowDialog() == true)
{
.....
}
}
}
}
<Window x:Class="MyProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MyProject"
mc:Ignorable="d"
Style="{StaticResource CustomWindowStyle}">
<Grid>
....
</Grid>
</Window>
<Application x:Class="MyProject.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SciDoxViewer.Commands"
xmlns:main="clr-namespace:MyProject"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MyProject;component/WindowStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
....
App.xaml:
<Window x:Class="MyProject.Commands.Person"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MyProject">
<Grid>
<Label>Last Name:</Label>
<TextBox Name="txtLastName">
<TextBox.Text>
<Binding Path="LastName" Source="{StaticResource prs}" UpdateSourceTrigger="PropertyChanged">
</Binding>
</TextBox.Text>
</TextBox>
</Grid>
</Window
namespace SciDoxViewer
{
public partial class WindowStyle : ResourceDictionary
{
public WindowStyle()
{
InitializeComponent();
}
//Here: click events for other buttons in above ResourceDictionary.xaml file
...............
...............
private void btnAddPerson_Click(object sender, RoutedEventArgs e)
{
Person person = new Person(); //This line first takes you to `InitializeComponent();` of the constructor of `Person` class where the above error occurs.
if (person.ShowDialog() == true)
{
.....
}
}
}
}
<Window x:Class="MyProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MyProject"
mc:Ignorable="d"
Style="{StaticResource CustomWindowStyle}">
<Grid>
....
</Grid>
</Window>
<Application x:Class="MyProject.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SciDoxViewer.Commands"
xmlns:main="clr-namespace:MyProject"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MyProject;component/WindowStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
您应该从资源字典中删除Person
资源,因为您已经在单击事件处理程序中创建了Person
窗口的另一个实例。该资源无论如何都没有被使用。我在您发布的代码中没有看到任何东西会将WindowStyle
资源字典放入要引用该资源的Person
窗口的搜索路径中。你凭什么认为它应该起作用?另外,请不要省略代码示例的部分。如果省略的代码不重要,请不要提及它。请提供一个自包含、最小且完整的实际值。@PeterDuniho在阅读了您的评论之后,我刚刚添加了Person.xaml
文件的
标记中缺少的部分。这不起作用。不能让XAML为的Person类包含使用同一类的实例作为源的绑定。CustomWindowsStyle应该在Person.xaml中声明。@nam:ResourceDictionary
在哪里合并/引用到窗口?@nam:Yes,这是正确的。