[英]Bind Dependency property to Image source
ToggleButton的C,用觸發器切換按鈕的圖像。 現在我想為圖像路徑創建DP並將其綁定到Image的源(通過BitmapImage,UriSource),所以當我使用UC時,我可以在xaml文件中設置兩個圖像的路徑。
下面的代碼運行但在窗口上升之前崩潰,拋出一個異常 - 'System.Windows.Media.Imaging.BitmapImage'引發異常。 內部異常 - “當對象的狀態不支持方法調用時調用對象的方法時拋出System.InvalidOperationException。當方法試圖從一個線程操作UI時,也拋出異常不是主線程或UI線程。“ 有人知道為什么它會如何解決它?
UC 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>
UC 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>
您正在嘗試在運行時更改BitmapImage
的UriSource
。 你不能這樣做,只需更改文件路徑即可 。 BitmapImage
實現接口ISupportInitialize並需要初始化。
這意味着,您必須每次都使用新源創建新的BitmapImage ,或者初始化它(通過調用BeginInit和EndInit )。
更改Uri
是不夠的,您必須更改整個BitmapImage
。
我曾經將MarkupExtension與一個轉換器結合起來處理這種情況。 它需要一個路徑或一個綁定,並可能需要一些修改才能在您的情況下工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.