我正在嘗試實作一個自定義控制元件庫專案以用于其他專案。我在那里 99% 并且一切正常,但是,我的自定義組框控制元件很挑剔,沒有洗掉標題間距來創建無縫框。下面是在造型的XAML Generic.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPFCommonControls">
<Style x:Key="GroupBoxStyleNoHeader" TargetType="{x:Type local:GroupBoxNoHeader}">
<Setter Property="BorderBrush" Value="#D5DFE5"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:GroupBoxNoHeader}">
<Grid SnapsToDevicePixels="true">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="6"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="6"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="6"/>
</Grid.RowDefinitions>
<Border Background="{TemplateBinding Background}" BorderBrush="Transparent" BorderThickness="{TemplateBinding BorderThickness}" Grid.Column="0" Grid.ColumnSpan="4" Grid.RowSpan="3" Grid.Row="1"/>
<Border BorderBrush="White" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="4" Grid.ColumnSpan="4" Grid.RowSpan="3" Grid.Row="1">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<Border BorderBrush="White" BorderThickness="{TemplateBinding BorderThickness}"/>
</Border>
</Border>
<Border x:Name="Header" Grid.Column="1" Padding="3,1,0,0" Grid.RowSpan="2" Grid.Row="0">
<ContentPresenter ContentSource="Header" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Border>
<ContentPresenter Grid.Column="1" Grid.ColumnSpan="2" Margin="{TemplateBinding Padding}" Grid.Row="2" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
該樣式主要只是我拉出并洗掉 OpacityMask 以擺脫標題空間的默認 GroupBox 實作。此方法在一個基本專案上 100% 有效,其中這都是本地的并包含在資源字典中。但是,現在我有一個參考我的自定義組框專案樣式的外部專案,它正在消失。組框呈現,但是,標題空間不會被洗掉。
這是我的自定義控制元件的cs:
namespace WPFCommonControls
{
/// <summary>
/// Follow steps 1a or 1b and then 2 to use this custom control in a XAML file.
///
/// Step 1a) Using this custom control in a XAML file that exists in the current project.
/// Add this XmlNamespace attribute to the root element of the markup file where it is
/// to be used:
///
/// xmlns:MyNamespace="clr-namespace:WPFCommonControls"
///
///
/// Step 1b) Using this custom control in a XAML file that exists in a different project.
/// Add this XmlNamespace attribute to the root element of the markup file where it is
/// to be used:
///
/// xmlns:MyNamespace="clr-namespace:WPFCommonControls;assembly=WPFCommonControls"
///
/// You will also need to add a project reference from the project where the XAML file lives
/// to this project and Rebuild to avoid compilation errors:
///
/// Right click on the target project in the Solution Explorer and
/// "Add Reference"->"Projects"->[Select this project]
///
///
/// Step 2)
/// Go ahead and use your control in the XAML file.
///
/// <MyNamespace:GroupBoxNoHeader/>
///
/// </summary>
public class GroupBoxNoHeader : GroupBox
{
static GroupBoxNoHeader()
{
//DefaultStyleKeyProperty.OverrideMetadata(typeof(GroupBoxNoHeader), new FrameworkPropertyMetadata(typeof(GroupBoxNoHeader)));
}
}
}
我注釋掉了 DefaultStyleKeyProperty 的東西,因為啟用它后我的控制元件將不再顯示。我覺得這可能是問題的一部分?但是,如果我保留它,那么整個組框就不會出現。
MainWindow在參考自定義控制元件專案的單獨專案中
<Window x:Class="TestWpfResources.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:CommonControls="clr-namespace:WPFCommonControls;assembly=WPFCommonControls"
xmlns:local="clr-namespace:TestWpfResources"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<CommonControls:GroupBoxNoHeader Margin="30,30,30,30">
</CommonControls:GroupBoxNoHeader>
</Grid>
</Window>
uj5u.com熱心網友回復:
當你呼叫DefaultStyleKeyProperty.OverrideMetadata你的類的靜態建構式,your're應該在定義該控制元件的默認風格ResourceDictionary稱為generic.xaml位于一個名為檔案夾themes在其中控制元件類是默認定義的專案的根。
因此,themes/generic.xaml如果您還沒有這樣做,請移動您的風格,并x:Key從以下內容中洗掉Style:
<Style TargetType="{x:Type local:GroupBoxNoHeader}">
...
如果您隨后像這樣定義自定義控制元件,則應始終應用您的樣式,前提是您未Style在應用程式的其他位置設定或以其他方式覆寫控制元件實體的屬性:
public class GroupBoxNoHeader : GroupBox
{
static GroupBoxNoHeader()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(GroupBoxNoHeader),
new FrameworkPropertyMetadata(typeof(GroupBoxNoHeader)));
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/409445.html
標籤:
