我是 wpf 用户控件的新手,我正在尝试创建一个用户控件作为按钮,该按钮具有一半的 .png 图像,其余部分有文本。
我设计了我的用户控件,但无法从 Windows 窗体设置图像和文本。我正在使用 ElementHost 以 Windows 形式调用我的用户控件。
谁能帮助我我做错了什么。
我的用户控制如下。
<UserControl x:Class="MyAssembly.SideNavigationButtons"
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"
xmlns:local="clr-namespace:MyAssembly"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<UserControl.Resources>
<Style x:Key="SideButton" TargetType="{x:Type Button}">
<Setter Property="Background" Value="#70b639"></Setter>
<Setter Property="Foreground" Value="#FF3A700F"></Setter>
<Setter Property="Height" Value="50"></Setter>
<Setter Property="Width" Value="200"></Setter>
<Setter Property="FontFamily" Value="segoe UI"></Setter>
<Setter Property="FontSize" Value="14"></Setter>
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border BorderBrush="#FF3A700F" BorderThickness="0,0,0,1">
<Grid Background="{TemplateBinding Background}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Image x:Name="imgSideIcon" Grid.Column="0" Margin="5,5,5,5" Source="{DynamicResource ResourceKey=ImageSource}" HorizontalAlignment="Left" VerticalAlignment="Center"/>
<ContentPresenter HorizontalAlignment="Left" Margin="5,5,5,5" VerticalAlignment="Center" Grid.Column="1"/>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Cursor" Value="Hand" />
<Setter Property="Foreground" Value="White"/>
<Setter Property="Background" Value="#FF3A700F"/>
</Trigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
<Grid DataContext="buttonsList">
<Button x:Name="btnSideBar" Click="btnSideBar_Click" Style="{StaticResource SideButton}" Content="Manage Master Data"></Button>
</Grid>
代码隐藏文件。
namespace MyAssembly
{
/// <summary>
/// Interaction logic for UserControl1.xaml
/// </summary>
public partial class SideNavigationButtons : UserControl
{
public string buttonContent = "";
public string buttonImagePath = "";
public event EventHandler ButtonClick;
ObservableCollection<CustomButtonObject> buttonsList = new ObservableCollection<CustomButtonObject>();
public void SetButtonContent(string imageName, string content)
{
var uri = new Uri("pack://application:,,,/BluePiControls;component/Images/"+ imageName + "");
var bitmap = new BitmapImage(uri);
CustomButtonObject obj = new CustomButtonObject();
obj.ButtonContent = content;
obj.ImageSource = bitmap;
buttonsList.Add(obj);
this.DataContext = buttonsList;
//btnSideBar.Content = content;
}
public SideNavigationButtons()
{
InitializeComponent();
}
private void btnSideBar_Click(object sender, RoutedEventArgs e)
{
if (ButtonClick != null)
ButtonClick(this, e);
}
}
}
文件后面的 Windows 窗体代码
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
BluePiControls.SideNavigationButtons sideButtonHD = ( BluePiControls.SideNavigationButtons)elementHost1.Child;
elementHost1.AutoSize = true;
sideButtonHD.SetButtonContent("Image1.png", "First Image");
BluePiControls.SideNavigationButtons sideButtonWH = (BluePiControls.SideNavigationButtons)elementHost2.Child;
elementHost2.AutoSize = true;
sideButtonWH.SetButtonContent("Image2.png", "Second Image");
}
}