我想创建一个以图像和文本块为内容的按钮。所以我开始寻找答案并找到了一个帖子(Reusable Custom Content for Buttons),它告诉我创建一个用户控件。
我这样做了,效果很好。我可以通过依赖属性设置图像源和文本。但是,我被卡住了,因为我的控件没有点击事件。
我做了更多的挖掘并得出结论,我可能需要一个从 Button 派生的 CustomControl。这个对吗?或者将点击事件连接到我的用户控件会更好吗?
这是我的用户控件:
<UserControl x:Class="Client.Usercontrols.MyButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" MinHeight="30" MinWidth="40"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Button Width="Auto" HorizontalAlignment="Center">
<Border CornerRadius="5" BorderThickness="1" BorderBrush="Transparent" >
<Grid>
<Image Name="tehImage" Source="{Binding ImageSource}" />
<TextBlock Name="tehText" Text="{Binding Text}"
Style="{DynamicResource ButtonText}" />
</Grid>
</Border>
</Button>
</UserControl>
执行
<my:MyButton ImageSource="../Images/MainSyncButton.png" ImageWidth="141" Text="Synchronise" Click="btnSynchronise_Click" />