C# WPF自定义窗口在设计时不应用其通用模板
我创建了一个简单的WPF自定义控件,该控件继承自Windows,以便以后我能够覆盖它的chrome并在我的项目中重用它。控件与generic.xaml文件一起包含在它自己的项目中(在Themes目录中)。控件的样式只是设置一个红色背景。在第二个项目(相同的解决方案)中,我使用该控件作为新WPF窗口的基类。现在,当我编译项目并让它运行时,它会以所需的红色背景显示我的(自定义)窗口 我现在的问题是:如何让WPF设计器在设计时显示通用样式(在本例中为红色背景) 当我对一个按钮执行相同的操作时,通用样式将在设计时自动应用。但从窗口继承的控件的任何默认样式只会在运行时显示。我错过了什么 控制代码:C# WPF自定义窗口在设计时不应用其通用模板,c#,wpf,xaml,controls,design-time,C#,Wpf,Xaml,Controls,Design Time,我创建了一个简单的WPF自定义控件,该控件继承自Windows,以便以后我能够覆盖它的chrome并在我的项目中重用它。控件与generic.xaml文件一起包含在它自己的项目中(在Themes目录中)。控件的样式只是设置一个红色背景。在第二个项目(相同的解决方案)中,我使用该控件作为新WPF窗口的基类。现在,当我编译项目并让它运行时,它会以所需的红色背景显示我的(自定义)窗口 我现在的问题是:如何让WPF设计器在设计时显示通用样式(在本例中为红色背景) 当我对一个按钮执行相同的操作时,通用样式
公共类CustomControl1:窗口
{
静态CustomControl1()
{
OverrideMetadata(typeof(CustomControl1),new FrameworkPropertyMetadata(typeof(CustomControl1));
}
}
generic.xaml:
我使用控件的第二个项目中测试窗口的XAML:
对于记录,我使用VS2013,项目使用框架4.5.1进行编译
Thx,
Ben试试另一种方法怎么样?不要从
窗口
继承自定义控件,而是尝试从ContentControl
继承它,然后在常规的窗口
中使用它作为顶级控件并定义其内容
自定义控件应该如下所示
namespace ProjectBar.Controls
{
public class CustomControl : ContentControl
{
static CustomControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl), new FrameworkPropertyMetadata(typeof(CustomControl)));
}
}
}
现在让我们在ProjectBar\Themes\Generic.xaml中定义默认控件模板
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:ProjectBar.Controls">
<Style TargetType="{x:Type controls:CustomControl}">
<Setter Property="Background" Value="Red"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type controls:CustomControl}">
<Grid Margin="5" Background="{TemplateBinding Background}">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<!--Define the ContentPresenter. This is the place where the Content goes-->
<ContentPresenter Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<!--Place the OK and Cancel buttons-->
<StackPanel Orientation="Horizontal" FlowDirection="RightToLeft" Grid.Row="1" >
<Button Content="Cancel" HorizontalAlignment="Right" Width="60" />
<Button Content="OK" HorizontalAlignment="Right" Width="60" />
</StackPanel>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
我花了很多时间试图解决这个问题 答案是: ---->将参考项目(控制项目)和消费项目的我的平台设置更改为任何Cpu 如果“控制项目”被设置为“任何Cpu”,并且您正试图在“消费项目”中使用x64,则会出现问题。
(对不起英语)我想这是一个众所周知的问题。这与你是否找到了解决这个问题的方法有关?我有同样的行为…六年后,我有同样的问题。就我而言,两人都没有提出答案。我在一个测试项目中定义了所有内容,因此没有需要处理的目标cpu设置差异。我尝试了各种cpu目标,但都没有成功。我必须使用Window作为我的自定义控件库,因为我正在用我自己的自定义外观替换WindowChrome,该窗口具有透明性和自定义事件处理程序,需要在窗口级别上运行。有没有人有幸在VisualStudio中获得自定义窗口控件以在设计时正确显示?(我目前使用的是VS2017,框架4.6.1)。
<Window x:Class="ProjectFoo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:ProjectBar.Controls;assembly=ProjectBar"
Title="MainWindow" Height="200" Width="300">
<controls:CustomControl>
<Label>Place anything here</Label>
</controls:CustomControl>
</Window>
<Application x:Class="ProjectFoo.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:ProjectBar.Controls;assembly=ProjectBar"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style TargetType="{x:Type controls:CustomControl}">
<Setter Property="Background" Value="Green"/>
</Style>
</Application.Resources>
</Application>
[assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)]