1
我有一個基於按鈕一個簡單的自定義控件:不能綁定自定義控制內容(WPF)
<Style TargetType="{x:Type local:ArrowButton}">
<Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/>
<Setter Property="BorderBrush" Value="{StaticResource NormalBorderBrush}"/>
<Setter Property="Focusable" Value="True"/>
<Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:ArrowButton}">
<Grid>
<Ellipse Fill="{TemplateBinding Background}" Stroke="{TemplateBinding BorderBrush}"/>
<ContentPresenter/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
它不會有任何顯著C#代碼:
public class ArrowButton : Button, IDisposable
{
static ArrowButton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ArrowButton), new FrameworkPropertyMetadata(typeof(ArrowButton)));
}
}
如果我用這個控制(有些用戶控件內)通過以下方式,它工作正常(我買了黑色三角形兩個按鈕):
<Grid.Resources>
<Style TargetType="local:ArrowButton">
<Setter Property="Width" Value="30"/>
<Setter Property="Height" Value="30"/>
</Style>
</Grid.Resources>
<local:ArrowButton Grid.Column="0" HorizontalAlignment="Left">
<Path HorizontalAlignment="Center" VerticalAlignment="Center" Fill="Black" Data="M 0 0 L 0 20 L 10 10 Z"/>
</local:ArrowButton>
<local:ArrowButton Grid.Column="1" HorizontalAlignment="Right">
<Path HorizontalAlignment="Center" VerticalAlignment="Center" Fill="Black" Data="M 0 0 L 0 20 L 10 10 Z"/>
</local:ArrowButton>
但如果我我的路徑移動到StaticR esource比我得到一個奇怪的效果:只有最後一個按鈕有黑色的三角形和UserConltrol上的所有其他按鈕保持空白(看起來像路徑綁定只有一次)。
<Grid.Resources>
<Path x:Key="ArrowPath" Fill="Black" Data="M 0 0 L 0 20 L 10 10 Z"/>
</Grid.Resources>
<local:ArrowButton Content="{StaticResource ArrowPath}" Grid.Column="0" HorizontalAlignment="Left"/>
<local:ArrowButton Content="{StaticResource ArrowPath}" Grid.Column="1" HorizontalAlignment="Left"/>
任何想法,有什麼可能是錯的?
非常感謝你! – Dmitriano