我有一個繼承自的 CustomComboBox 組件,ComboBox其樣式復制到另一個資源字典 xaml 檔案。
問題是,我可以或如何從 sub 樣式中為ComboBoxToggleButtonused in ComboBoxTemplateused in系結屬性CustomComboBoxStyle?
的屬性是:GryphlBrush在
<路徑X:名稱= “箭頭”中風= “{結合GryphlBrush}” ...
下面的代碼示例:
<Style x:Key="ComboBoxToggleButton" TargetType="{x:Type ToggleButton}">
<!-- ... -->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
Border x:Name="templateRoot" SnapsToDevicePixels="true" Background="Transparent" BorderThickness="0" BorderBrush="Transparent" CornerRadius="4">
<Border x:Name="splitBorder" Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" SnapsToDevicePixels="true" Margin="0" HorizontalAlignment="Right" BorderThickness="1" BorderBrush="Transparent">
<Path x:Name="arrow" Stroke="{Binding GryphlBrush}" VerticalAlignment="Center" Margin="0" HorizontalAlignment="Center" Fill="{StaticResource ComboBox.Static.Glyph}" Data="F1 M 0,0 L 2.667,2.66665 L 5.3334,0 L 5.3334,-1.78168 L 2.6667,0.88501 L0,-1.78168 L0,0 Z"/>
</Border>
</Border>
<!-- ... -->
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<ControlTemplate x:Key="ComboBoxTemplate" TargetType="{x:Type ComboBox}">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="4">
<Grid x:Name="templateRoot" SnapsToDevicePixels="true">
<!-- ... -->
<ToggleButton x:Name="toggleButton" BorderBrush="Transparent" BorderThickness="0" Background="Transparent" Grid.ColumnSpan="2" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource ComboBoxToggleButton}"/>
<!-- ... -->
</Grid>
</Border>
<!-- ... -->
</ControlTemplate>
<Style x:Key="BorderedComboBoxStyles" TargetType="{x:Type components:BorderedComboBox}">
<Setter Property="Template" Value="{StaticResource ComboBoxTemplate}"/>
</Style>
在后面的代碼中,我只定義了依賴屬性、getter 和 setter:
public class BorderedComboBox : ComboBox
{
public static readonly DependencyProperty GryphlBrushDP = DependencyProperty.Register(
"GryphlBrush", typeof(Brush), typeof(BorderedComboBox));
private Brush _gryphlBrush;
public Brush GryphlBrush
{
get => _gryphlBrush;
set => _gryphlBrush = value;
}
}
uj5u.com熱心網友回復:
您應該遵守依賴屬性及其 getter 和 setter的命名約定。此外,使用nameoffor 屬性可以防止重命名時出錯。
用于存盤依賴屬性的名稱和特征的識別符號欄位的名稱必須是您在注冊呼叫中為依賴屬性選擇的 Name,并附加文字 string
Property。[...]如果您未能遵循此命名模式,設計人員可能無法正確報告您的屬性,并且屬性系統樣式應用程式的某些方面可能無法按預期運行。
因此,您的依賴屬性定義應如下所示:
public class BorderedComboBox : ComboBox
{
public static readonly DependencyProperty GryphlBrushProperty = DependencyProperty.Register(
nameof(GryphlBrush), typeof(Brush), typeof(BorderedComboBox));
private Brush _gryphlBrush;
public Brush GryphlBrush
{
get => _gryphlBrush;
set => _gryphlBrush = value;
}
}
如果要系結GryphlBrush屬性,在ToggleButton控制元件模板中,可以:
創建自定義切換按鈕和一個依賴屬性添加到它,然后將其系結到
GryphlBrush財產上的BorderedComboBox。如果切換按鈕僅在組合框中的此背景關系中使用,請使用 應用
RelativeSource系結AncestorType,因為BorderedComboBox是切換按鈕的父級。<Style x:Key="ComboBoxToggleButton" TargetType="{x:Type ToggleButton}"> <!--...--> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ToggleButton}"> <Border x:Name="templateRoot" SnapsToDevicePixels="true" Background="Transparent" BorderThickness="0" BorderBrush="Transparent" CornerRadius="4"> <Border x:Name="splitBorder" Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" SnapsToDevicePixels="true" Margin="0" HorizontalAlignment="Right" BorderThickness="1" BorderBrush="Transparent"> <!--<Path x:Name="arrow" Stroke="{Binding GryphlBrush, RelativeSource={RelativeSource AncestorType={x:Type local:BorderedComboBox}}}" VerticalAlignment="Center" Margin="0" HorizontalAlignment="Center" Fill="{StaticResource ComboBox.Static.Glyph}" Data="F1 M 0,0 L 2.667,2.66665 L 5.3334,0 L 5.3334,-1.78168 L 2.6667,0.88501 L0,-1.78168 L0,0 Z"/>--> <Path x:Name="arrow" Stroke="{Binding GryphlBrush, RelativeSource={RelativeSource AncestorType={x:Type local:BorderedComboBox}}}" VerticalAlignment="Center" Margin="0" HorizontalAlignment="Center" Data="F1 M 0,0 L 2.667,2.66665 L 5.3334,0 L 5.3334,-1.78168 L 2.6667,0.88501 L0,-1.78168 L0,0 Z"/> </Border> </Border> <!--...--> </ControlTemplate> </Setter.Value> </Setter> </Style>
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/334475.html
下一篇:預選單選按鈕WPFMVVM
