0
我想創建我的按鈕樣式(StaticResources)和圖標(DynamicResources)。我不知道,如何綁定資源。我可以將StaticResource綁定爲對象。 這是我的代碼:WPF自定義按鈕 - 綁定動態和靜態資源
<Button x:Class="Spd.Client.View.Controls.IconButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" Style="{Binding ButtonStyle, UpdateSourceTrigger=PropertyChanged}">
<StackPanel Orientation="Horizontal">
<Rectangle Width="12" Height="12" Fill="{Binding Path=Foreground,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}">
<Rectangle.OpacityMask>
<VisualBrush Stretch="Fill" Visual="{Binding Icon, UpdateSourceTrigger=PropertyChanged}"/>
</Rectangle.OpacityMask>
</Rectangle>
<TextBlock Text="{Binding Text, UpdateSourceTrigger=PropertyChanged}" FontSize="10" Margin="10 0 0 0"/>
</StackPanel>
</Button>
後面的代碼:
public partial class IconButton : Button, INotifyPropertyChanged
{
private string _text;
public string Text
{
get { return _text; }
set { _text = value; OnPropertyChanged(nameof(Text)); }
}
private object _style;
public object ButtonStyle
{
get { return _style; }
set { _style = value; OnPropertyChanged(nameof(Style));}
}
public ??? Icon //which datatype?
{
get { }
set
{
}
}
public IconButton()
{
InitializeComponent();
DataContext = this;
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
使用:
<controls1:IconButton Text="Click me!" ButtonStyle="{StaticResource DangerButton}"
Icon="{DynamicResource appbar_user}"></controls1:IconButton>
你能告訴我們什麼是你的偶像?如果你使用它作爲'StaticResource',它是否工作? – XAMlMAX
只要去一個簡單的模板。 –