通用 wpf 中的资源 window
Resources in generic wpf window
我正在尝试在 WPF 中使用泛型 windows。我知道如何创建它们,但我很难在这样的 window 中指定资源或根元素中的任何其他内容。这可能吗?您只能在根标记中使用 x:TypeArguments,所以恐怕如果没有通用规范,根标记之外的任何定义都不会被识别和处理。我在这里遗漏了什么?
这是一个最小的(非)功能代码:
MainWIndow.xaml
<local:ViewBase x:Class="WpfApp1.MainWindow"
x:TypeArguments="local:TestViewModel"
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:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<local:ViewBase.Resources>
<ResourceDictionary>
<Style x:Key="TEST" TargetType="TextBlock">
<Setter Property="Foreground" Value="Red"/>
</Style>
</ResourceDictionary>
</local:ViewBase.Resources>
<Grid>
<TextBlock Text="TEST" Style="{StaticResource TEST}" />
</Grid>
MainWindow.xaml.cs
public partial class MainWindow : ViewBase<TestViewModel>
{
public MainWindow()
: base(null)
{
InitializeComponent();
}
public MainWindow(TestViewModel vm)
: base(vm)
{
InitializeComponent();
}
}
SomeClass.cs
public class ViewBase<T> : Window where T : ViewModelBase
{
public ViewBase(T vm)
{
DataContext = vm;
}
}
public class ViewModelBase
{
public ViewModelBase()
{
}
}
public class TestViewModel : ViewModelBase
{
public TestViewModel()
{
}
}
直接写Window.Resources
:
<local:ViewBase ...>
<Window.Resources>
<ResourceDictionary>
<Style x:Key="TEST" TargetType="TextBlock">
<Setter Property="Foreground" Value="Red"/>
</Style>
</ResourceDictionary>
</Window.Resources>
<Grid>
<TextBlock Text="TEST" Style="{StaticResource TEST}" />
</Grid>
</local:ViewBase>
我正在尝试在 WPF 中使用泛型 windows。我知道如何创建它们,但我很难在这样的 window 中指定资源或根元素中的任何其他内容。这可能吗?您只能在根标记中使用 x:TypeArguments,所以恐怕如果没有通用规范,根标记之外的任何定义都不会被识别和处理。我在这里遗漏了什么?
这是一个最小的(非)功能代码:
MainWIndow.xaml
<local:ViewBase x:Class="WpfApp1.MainWindow"
x:TypeArguments="local:TestViewModel"
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:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<local:ViewBase.Resources>
<ResourceDictionary>
<Style x:Key="TEST" TargetType="TextBlock">
<Setter Property="Foreground" Value="Red"/>
</Style>
</ResourceDictionary>
</local:ViewBase.Resources>
<Grid>
<TextBlock Text="TEST" Style="{StaticResource TEST}" />
</Grid>
MainWindow.xaml.cs
public partial class MainWindow : ViewBase<TestViewModel>
{
public MainWindow()
: base(null)
{
InitializeComponent();
}
public MainWindow(TestViewModel vm)
: base(vm)
{
InitializeComponent();
}
}
SomeClass.cs
public class ViewBase<T> : Window where T : ViewModelBase
{
public ViewBase(T vm)
{
DataContext = vm;
}
}
public class ViewModelBase
{
public ViewModelBase()
{
}
}
public class TestViewModel : ViewModelBase
{
public TestViewModel()
{
}
}
直接写Window.Resources
:
<local:ViewBase ...>
<Window.Resources>
<ResourceDictionary>
<Style x:Key="TEST" TargetType="TextBlock">
<Setter Property="Foreground" Value="Red"/>
</Style>
</ResourceDictionary>
</Window.Resources>
<Grid>
<TextBlock Text="TEST" Style="{StaticResource TEST}" />
</Grid>
</local:ViewBase>