【问题标题】:How to Pass Image to a Button in UserControl using Dependency Property如何使用依赖属性将图像传递给 UserControl 中的按钮
【发布时间】:2014-08-18 05:31:35
【问题描述】:

我创建了一个用户控件。它有一个按钮和一个文本块。我想使用 Dependency 属性将图像传递给该按钮。但是当我尝试在其他页面中调用该用户控件时,它会显示一些错误。这是我的代码..

用户 Control.xaml:

<UserControl x:Class="....UserControl"
         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" 
         d:DesignHeight="300" d:DesignWidth="300" >
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <Border Grid.Row="0"
        Name="borMain"
        Style="{StaticResource ButtonImageTextBorderStyle}"
        MouseEnter="borMain_MouseEnter"
        MouseLeave="borMain_MouseLeave"
        PreviewMouseLeftButtonDown="borMain_MouseLeftButtonDown" >
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup Name="MouseStates">
                <VisualState Name="MouseEnter">
                    <Storyboard>
                        <ColorAnimation To="Black"
                          Duration="0:0:00.1"
                          Storyboard.TargetName="borMain"
                          Storyboard.TargetProperty="BorderBrush.Color" />
                        <ColorAnimation To="Black"
                          Duration="0:0:00.1"
                          Storyboard.TargetName="borMain"
                          Storyboard.TargetProperty="Background.Color" />
                        <ThicknessAnimation To="4,1,4,4"
                              Duration="0:0:00.1"
                              Storyboard.TargetName="borMain"
                              Storyboard.TargetProperty="Margin" />
                    </Storyboard>
                </VisualState>
                <VisualState Name="MouseLeave" />
                <VisualStateGroup.Transitions>
                    <VisualTransition To="MouseLeave" />
                    <VisualTransition To="MouseEnter" />
                </VisualStateGroup.Transitions>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
        <Button Content="{Binding Path=AppBarContent}"                  
         Style="{StaticResource ButtonImageTextImageStyle}" />
    </Border>

    <TextBlock Grid.Row="1"
           Name="tbText"
           Style="{StaticResource ButtonImageTextTextBlockStyle}"
           Text="{Binding Path=Text}" />
   </Grid>
</UserControl>

UserControl.xaml.cs:

 [DefaultEvent("Click")]
   public partial class SystemUnitUserControl : UserControl
   {
      public UserControl()
      {
        InitializeComponent();
        this.DataContext = this;
      }
    #region Text Property
    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set
        {
            SetValue(TextProperty, value);
            if (string.IsNullOrEmpty(value))
                tbText.Visibility = Visibility.Collapsed;
        }
    }


    #region AppBarContent Property
    public Image AppBarContent
    {
        get { return (Image)GetValue(AppBarContentProperty); }
        set { SetValue(AppBarContentProperty, value); }
    }

      public static readonly DependencyProperty AppBarContentProperty =
        DependencyProperty.Register("AppBarContent", typeof(Image),  typeof(SystemUnitUserControl), null);
    #endregion

    #region MouseLeftDown Event
    private void borMain_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        RaiseClick(e);
    }
    #endregion

    #region Click Event Procedure
    public delegate void ClickEventHandler(object sender, RoutedEventArgs e);
    public event ClickEventHandler Click;

    protected void RaiseClick(RoutedEventArgs e)
    {
        if (null != Click)
            Click(this, e);
    }
    #endregion

    #region Visual State Animations
    private void borMain_MouseEnter(object sender, MouseEventArgs e)
    {
        VisualStateManager.GoToElementState(borMain, "MouseEnter", true);
    }

    private void borMain_MouseLeave(object sender, MouseEventArgs e)
    {
        VisualStateManager.GoToElementState(borMain, "MouseLeave", true);
    }
    #endregion

我在其他页面中调用此用户控件。

Window1.xaml:

...
 xmlns:my="clr-namespace:....UserControls"
...

         <my:UserControl x:Name="actionRectDuct"   
                         AppBarContent="F:\..\..\Assets\offline.jpg"
                         Text="Button 1" />                         

它不会进入那个页面。它显示以下错误..

在 PresentationFramework.dll 中发生了“System.Windows.Markup.XamlParseException”类型的第一次机会异常

附加信息:“设置属性 'UnitUserControl.AppBarContent' 引发异常。”行号“38”和行位置“41”。

我想在 AppBarContent 上传递图像。我该怎么做?

【问题讨论】:

  • AppBarContent 的类型为 Image,您将其设置为字符串。
  • #region AppBarContent 属性 public Image AppBarContent { get { return (Image)GetValue(AppBarContentProperty); } set { SetValue(AppBarContentProperty, value); } } public static readonly DependencyProperty AppBarContentProperty = DependencyProperty.Register("AppBarContent", typeof(Image), typeof(SystemUnitUserControl), null); #endregion
  • 不。我设置为仅图像.. 我没有明白你的意思
  • 如果你想传递一个文件名,也许你可以考虑ImageSource而不是Image。 ImageSource 具有来自字符串的转换器。或者你可以编写一个转换器将图像路径字符串转换为图像。
  • @JohnPaul - 文件路径和图像是完全不同的东西。您需要传递使用文件路径创建的 Image 对象。将 DP 更改为类型字符串或从 UI 绑定 Image 对象。

标签: c# wpf xaml user-controls


【解决方案1】:

这里是你如何对你的代码做同样的事情

     <my:UserControl x:Name="actionRectDuct"
                     Text="Button 1" >   
         <my:UserControl.AppBarContent>
             <Image Source="F:\..\..\Assets\offline.jpg" />
         </my:UserControl.AppBarContent>
     </my:UserControl>

我们现在将传递具有源作为所需文件的 Image 实例,而不是将字符串传递给接受 Image 的 AppBarContent 属性。因此只要来源正确,您就可以在 SystemUnitUserControl 中获得所需的图像

【讨论】:

  • 我无法为此用户控件使用 Click 事件。你能帮我为我的 UserControl 分配 Click 事件吗???
  • 我很高兴它对你有用。现在您有一个新问题,我可以要求您发布一个包含必要详细信息的单独问题。就像你在哪里定义的按钮?您想在哪里接收事件(事件处理程序)?和任何其他必要的细节。谢谢
猜你喜欢
  • 2013-01-14
  • 2014-03-01
  • 1970-01-01
  • 2015-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-24
相关资源
最近更新 更多