[英]How to "inject" a different ControlTemplate in a CustomControl?
我有一個 CustomControl 的 WPF 項目。 它有 a.cs 和一些依賴屬性:
namespace CustomControlLib
{
public class CustomControl1 : Control
{
static CustomControl1()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1), new FrameworkPropertyMetadata(typeof(CustomControl1)));
}
public Style IcStyle
{
get { return (Style)GetValue(IcStyleProperty); }
set { SetValue(IcStyleProperty, value); }
}
public static readonly DependencyProperty IcStyleProperty =
DependencyProperty.Register("IcStyle", typeof(Style), typeof(CustomControl1));
.....
和 XAML 代碼用於其 Generic.xaml 中的兩個ControlTemplates:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ic="clr-namespace:CustomControlLib">
<Style x:Key="ListBoxInputControl" TargetType="{x:Type ic:CustomControl1}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ic:CustomControl1}">
<Grid x:Name="ListRoot">
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="Labels"/>
<ColumnDefinition Width="320"/>
</Grid.ColumnDefinitions>
<Label
x:Name="PART_NameLabel2"
Grid.Column="0"
Margin="1">
<Label.Content>
...
</Label.Content>
</Label>
<ListBox
Grid.Column="1"
ItemsSource="{TemplateBinding ...}"
SelectedIndex="{TemplateBinding ...}"
SelectedItem="{TemplateBinding ...}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="ComboBoxInputControl" TargetType="{x:Type ic:CustomControl1}">
<Setter Property="SnapsToDevicePixels" Value="true" />
<Setter Property="OverridesDefaultStyle" Value="true" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ic:CustomControl1}">
<Grid
x:Name="ComboRoot">
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="Labels"/>
<ColumnDefinition Width="120"/>
</Grid.ColumnDefinitions>
<Label
Grid.Column="0"
<Label.Content>
...
</Label.Content>
</Label>
<ComboBox
x:Name="PART_ComboBox"
Grid.Column="1"
DisplayMemberPath="{TemplateBinding ...}"
ItemsSource="{TemplateBinding ...}"
SelectedItem="{TemplateBinding ...}">
</ComboBox>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
(還有第三個,用於文本框)
我想像這樣使用 CustomControl:
<Window
x:Class="CoCa.Views.MainWindow"
...
xmlns:ic="clr-namespace:CustomControlLib;assembly=CustomControlLib">
...
<ic:CustomControl1
Name1="PLF"
IcSelectedIndex="{Binding Path=.....}"
IcItemsSource="{Binding Source=...}"
IcStyle="{StaticResource ic:ListBoxInputControl}"/>
<ic:CustomControl1
Name1="PLF"
IcSelectedIndex="{Binding Path=.....}"
IcItemsSource="{Binding Source=...}"
IcStyle="{StaticResource ic:ComboBoxInputControl}"/>
我希望能夠像這樣在 CustomControl1 中插入正確的 ControlTemplate。 但是,唉:它不是這樣工作的。 我收到消息:“無法解析資源“ic:ComboBoxInputControl”。”
它是如何工作的?
嘗試使用其x:Key
引用Style
:
IcStyle="{StaticResource ComboBoxInputControl}"/>
您還需要將定義 styles 的ResourceDictionary
合並到 Window 的Window
中,例如:
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/CustomControlLib;component/themes/generic.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.