将依赖属性绑定到图像源

时间:2015-02-25 14:33:36

标签: c# wpf xaml

ToggleButton的C,用触发器切换按钮的图像。现在我想为图像路径创建DP并将其绑定到Image的源(通过BitmapImage,UriSource),所以当我使用U.C时,我可以在xaml文件中设置两个图像的路径。

以下代码运行但在窗口上升之前崩溃,抛出异常 - 'System.Windows.Media.Imaging.BitmapImage'抛出异常。 内部异常 - “当对象的状态不支持方法调用时调用对象的方法时抛出System.InvalidOperationException。当方法试图从一个线程操作UI时,也抛出异常不是主线程或UI线程。“ 有人知道为什么它会如何解决它?

U.C xaml文件:

UserControl x:Class="ButtonChangeImage.UserControl1"
             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">
    <Grid>
        <ToggleButton x:Name="btnCI">
            <ToggleButton.Content >
                <Image Name="img" Source="C:\Users\AmitL\Desktop\joecocker.jpg"/>
            </ToggleButton.Content>
            <ToggleButton.Triggers>
                <EventTrigger RoutedEvent="ToggleButton.Checked">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard>
                                <ObjectAnimationUsingKeyFrames Storyboard.Target="{x:Reference img}" Storyboard.TargetProperty="Source">
                                    <DiscreteObjectKeyFrame KeyTime="0:0:0">
                                        <DiscreteObjectKeyFrame.Value>
                                            <BitmapImage UriSource="{Binding ElementName=FirstImage}"/>
                                            <!--<BitmapImage UriSource="C:\Users\AmitL\Desktop\james-brown-010.jpg"/>--><!--if I switch to this line it works fine!-->
                                        </DiscreteObjectKeyFrame.Value>
                                    </DiscreteObjectKeyFrame>
                                </ObjectAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger.Actions>
                </EventTrigger>

            </ToggleButton.Triggers>
        </ToggleButton>

    </Grid>
</UserControl>

U.C cs文件:

public partial class UserControl1 : UserControl
    {
        public static readonly DependencyProperty FirstImageDP = DependencyProperty.Register("FirstImage", typeof(string), typeof(UserControl1), new PropertyMetadata(@"C:\Users\AmitL\Desktop\james-brown-010.jpg", new PropertyChangedCallback(FirstImageSource)));
        private string m_strFirstImage = string.Empty;

        private static void FirstImageSource(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            UserControl1 l_UCBtnSwitchImage = (UserControl1)obj;
            l_UCBtnSwitchImage.m_strFirstImage = (string)args.NewValue;
        }

        public string FirstImage
        {
            get
            {
                return (string)GetValue(FirstImageDP);
            }
            set
            {
                SetValue(FirstImageDP, value);
            }
        }

        public event RoutedEventHandler Click
        {
            add { btnCI.Click += value; }
            remove { btnCI.Click -= value; }
        }

        public UserControl1()
        {
            InitializeComponent();
        }
    }

xaml窗口:

<Window x:Class="ButtonChangeImage.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:m="clr-namespace:ButtonChangeImage"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <m:UserControl1 Click="ToggleButton_Checked" FirstImage="C:\Users\AmitL\Desktop\james-brown-010.jpg"></m:UserControl1>
    </Grid>
</Window>

1 个答案:

答案 0 :(得分:0)

您正尝试在运行时更改UriSource的{​​{1}}。 You can't do it like this, by simply changing the filepathBitmapImage实现接口ISupportInitialize并需要初始化。

这意味着,您必须create the new BitmapImage with the new source every time或初始化它(通过调用BeginInitEndInit)。

更改BitmapImage是不够的,您必须更改整个Uri

我曾经做过MarkupExtension combined with a Converter来处理这种情况。它需要一个路径或一个绑定,可能需要一些修改才能在你的情况下工作。