簡體   English   中英

綁定到xaml中另一個控件樣式定義的默認屬性值

[英]Bind to default property value of another control style definition in xaml

我有一個CustomControl並且想要具有TextBox默認樣式定義的默認值。 我不知道那是否有可能。

到目前為止,這是我一直未嘗試的嘗試。 但是在這里您可以看到我的想法。

我的默認實現可更改TextBox的默認樣式(例如,在app.xaml中將其全局應用)

<Style TargetType="TextBox">
    <Setter Property="BorderThickness" Value="2"/>
</Style>

在這里,我想獲取值“ 2”。

<Style TargetType="controls:CustomControl">
    <Setter Property="BorderThickness" Value="{Binding Source={x:Static TextBox.BorderThicknessProperty}}"/>
</Style>

該默認值BorderThickness一個財產TextBox在默認樣式定義的TextBox ,其最終被應用到的實例TextBox在運行時。

因此,您不能執行以下操作:

<Style TargetType="controls:CustomControl">
    <Setter Property="BorderThickness" Value="{Binding Source={x:Static TextBox.BorderThicknessProperty}}"/>
</Style>

...除非您綁定到使用默認樣式的TextBox的實際實例。

您可以查看TextBox的默認模板,並只需復制其默認BorderThickness屬性值1

<Style TargetType="controls:CustomControl">
    <Setter Property="BorderThickness" Value="1"/>
</Style>

顯然,您還可以從多個Style綁定到一個類的屬性,例如:

<Style TargetType="controls:CustomControl">
    <Setter Property="BorderThickness" Value="{Binding Thickness, Source={StaticResource settings}}"/>
</Style>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM