111,092
社区成员




<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
x:Name="main"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Title="Window1" Height="300" Width="300">
<Grid>
<Image x:Name="image" Source="{Binding ImageSource1}"></Image>
</Grid>
</Window>
private BitmapImage imageSource1;
public BitmapImage ImageSource1
{
get
{
return imageSource1;
}
set
{
imageSource1 = value;
if (null != PropertyChanged)
{
PropertyChanged.Invoke(this, new PropertyChangedEventArgs("ImageSource1"));
}
}
}
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button x:Name="button" Content="Button" HorizontalAlignment="Left" Width="75" Click="button_Click"/>
</Grid>
</Window>
private void button_Click(object sender, RoutedEventArgs e)
{
BitmapImage imageSource1 = new BitmapImage();
imageSource1.BeginInit();
imageSource1.CacheOption = BitmapCacheOption.OnLoad;
imageSource1.StreamSource = new MemoryStream(File.ReadAllBytes("TestImage.png"));
imageSource1.EndInit();
Window1 w1 = new Window1();
w1.ImageSource1 = imageSource1;
w1.ShowDialog();
//BindingOperations.ClearBinding(w1.image, System.Windows.Controls.Image.SourceProperty);
}
private void button_Click(object sender, RoutedEventArgs e)
{
BitmapImage imageSource1 = new BitmapImage();
imageSource1.BeginInit();
imageSource1.CacheOption = BitmapCacheOption.OnLoad;
imageSource1.StreamSource = new MemoryStream(File.ReadAllBytes("TestImage.png"));
imageSource1.EndInit();
Window1 w1 = new Window1();
w1.ImageSource1 = imageSource1;
w1.ShowDialog();
BindingOperations.ClearBinding(w1.image, System.Windows.Controls.Image.SourceProperty);
}
BindingOperations.ClearAllBindings(w1);
public DelegateCommand<object> AppClosingCommand
{
get
{
return new DelegateCommand<object>((a) =>
{
//ImageSource1 = null;
ImageSource1.ClearValue(Image.SourceProperty);
GC.Collect();
GC.WaitForPendingFinalizers();
});
}
}
<Image x:Name="image1" Grid.Row="1" Source="{Binding ImageSource1, Mode=OneWay}">
<Image.Style>
<Style TargetType="{x:Type Image}">
<Style.Triggers>
<DataTrigger Binding="{Binding ImageSource1, Converter={StaticResource NullToBoolenConverter}}" Value="True">
<Setter Property="Source">
<Setter.Value>
<MultiBinding Converter="{StaticResource ReleaseImageConverter}">
<Binding Path="ImageSourceNULL"/>
<Binding ElementName="image1" />
</MultiBinding>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
public class ReleaseImageConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if ((BitmapImage)values[0] == null)
{
System.Windows.Controls.Image image = (Image)values[1] as System.Windows.Controls.Image;
BindingOperations.ClearBinding(image, System.Windows.Controls.Image.SourceProperty);
return DependencyProperty.UnsetValue; ;
}
else
{
return DependencyProperty.UnsetValue; ;
}
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
<Image x:Name="image1" Grid.Row="1" Source="{Binding ImageSource1, Mode=OneWay}">
<Image.Style>
<Style TargetType="{x:Type Image}">
<Style.Triggers>
<DataTrigger Binding="{Binding ImageSource1, Converter={StaticResource NullToBoolenConverter}}" Value="True">
<Setter Property="Source" Value="{x:Null}" />
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
public class NullToBoolenConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((BitmapImage)value == null)
{
return true;
}
else
{
return false;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class Window1VM : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private BitmapImage imageSource1;
public BitmapImage ImageSource1
{
get
{
return imageSource1;
}
set
{
imageSource1 = value;
if (null != PropertyChanged)
{
PropertyChanged.Invoke(this, new PropertyChangedEventArgs("ImageSource1"));
}
}
}
public Window1VM()
{
BitmapImage imageSource1 = new BitmapImage();
imageSource1.BeginInit();
imageSource1.CacheOption = BitmapCacheOption.OnLoad;
imageSource1.StreamSource = new MemoryStream(File.ReadAllBytes("TestImage.png"));
imageSource1.EndInit();
ImageSource1 = imageSource1;
}
public DelegateCommand<object> AppClosingCommand
{
get
{
return new DelegateCommand<object>((a) =>
{
//ImageSource1 = null;
ImageSource1.ClearValue(Image.SourceProperty);
GC.Collect();
GC.WaitForPendingFinalizers();
});
}
}
}
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
x:Name="main"
Title="Window1" Height="300" Width="300">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Closing">
<i:InvokeCommandAction Command="{Binding AppClosingCommand}">
</i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
<Grid>
<Image x:Name="image" Source="{Binding ImageSource1}"></Image>
</Grid>
</Window>
private void button_Click(object sender, RoutedEventArgs e)
{
Window1 w1 = new Window1();
Window1VM w1VM = new Window1VM();
w1.DataContext = w1VM;
w1.ShowDialog();
//BindingOperations.ClearBinding(w1.image, System.Windows.Controls.Image.SourceProperty);
}
BindingOperations.ClearBinding(w1.image, System.Windows.Controls.Image.SourceProperty);
//ImageSource1 = null;
ImageSource1.ClearValue(Image.SourceProperty);
GC.Collect();
GC.WaitForPendingFinalizers();
Binding RelativeSource
声明造成的,并不是 Window1 以及 MVVM 本身有问题。