Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法从windows窗体将imagePath和文本设置到按钮用户控件中_C#_Wpf_User Controls_Elementhost - Fatal编程技术网

C# 无法从windows窗体将imagePath和文本设置到按钮用户控件中

C# 无法从windows窗体将imagePath和文本设置到按钮用户控件中,c#,wpf,user-controls,elementhost,C#,Wpf,User Controls,Elementhost,我是wpf用户控件的新手,我正在尝试创建一个用户控件作为按钮,它有一半。png图像和其余部分有文本 我已设计了用户控件,但无法从windows窗体设置图像和文本。我正在使用ElementHost在windows窗体中调用我的用户控件 谁能帮我一下我做错了什么 我的用户控件如下。 <UserControl x:Class="MyAssembly.SideNavigationButtons" xmlns="http://schemas.microsoft.com/winfx/

我是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);
        }
     }
  }
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");

    }
}