我的应用程序中有一个Ellipse
,其中充满了ImageBrush
。我试图绑定ImageBrush.ImageSource
并在单击Ellipse
时进行更改。我正在使用实现ConvertImage
的类IValueConverter
来转换数据。但是,当我更改数据并调用函数PropertiesChanged时,它不会调用类ConvertImage
并且不会提醒数据。
这是我的代码Xaml:
<Window x:Class="CoffeeManager.Controls.CategoryControl.CategoryAddFoodForm"
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:CoffeeManager.Controls.CategoryControl"
xmlns:coffeeManager="clr-namespace:CoffeeManager"
xmlns:foodView="clr-namespace:CoffeeManager.Controls.FoodControl"
mc:Ignorable="d"
Style="{StaticResource WindowStyle}"
Title="Add Food To Categoory" Height="600" Width="1000" Background="#1e1e1e" Loaded="CategoryAddFoodForm_OnLoaded">
<Grid>
<Grid.Resources>
<coffeeManager:ConvertImage x:Key="ConvertImage"/>
</Grid.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="150"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Ellipse Grid.Column="1" Margin="10" StrokeThickness="2" Stroke="White" MouseDown="UIElement_OnMouseDown">
<Ellipse.Fill>
<ImageBrush Stretch="Uniform" ImageSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:CategoryViewForm}}, Path=Category.Image, Converter={StaticResource ConvertImage}, UpdateSourceTrigger=PropertyChanged}"/>
</Ellipse.Fill>
</Ellipse>
</Grid>
</Grid>
</Window>
这是后面的代码:
namespace CoffeeManager.Controls.CategoryControl
{
/// <summary>
/// Interaction logic for CategoryAddFoodForm.xaml
/// </summary>
public partial class CategoryAddFoodForm : Window, INotifyPropertyChanged
{
private Category category;
public Category Category
{
get => category;
set
{
category = value;
OnPropertiesChanged("Category");
}
}
public CategoryAddFoodForm()
{
InitializeComponent();
}
public event PropertyChangedEventHandler PropertyChanged;
public virtual void OnPropertiesChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
private void UIElement_OnMouseDown(object sender, MouseButtonEventArgs e)
{
var of = new OpenFileDialog
{
Filter = StaticValue.FilterImage
};
if (of.ShowDialog() == true)
{
if (!Directory.Exists(Environment.CurrentDirectory + StaticValue.PathDirectoryCategory)) Directory.CreateDirectory(Environment.CurrentDirectory + StaticValue.PathDirectoryCategory);
if (string.IsNullOrEmpty(category.Image) || !category.Image.Contains(StaticValue.OtherCode))
{
File.Copy(of.FileName, Environment.CurrentDirectory + StaticValue.PathDirectoryCategory + category.Name + StaticValue.CategoryExtensions, true);
category.Image = StaticValue.PathDirectoryCategory + category.Name + StaticValue.CategoryExtensions;
}
else
{
File.Copy(of.FileName, Environment.CurrentDirectory + StaticValue.PathDirectoryCategory + category.Name + StaticValue.OtherCode + StaticValue.CategoryExtensions, true);
category.Image = StaticValue.PathDirectoryCategory + category.Name + StaticValue.OtherCode + StaticValue.CategoryExtensions;
}
OnPropertiesChanged("Category"); //when I call Propertieschanged here it do nothing
}
}
}
}
最后这里是类ConvertImage:
public class ConvertImage : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
{
return null;
}
if (string.IsNullOrEmpty(value.ToString()))
{
return null;
}
try
{
return new BitmapImage(new Uri(Environment.CurrentDirectory + value.ToString()));
}
catch { }
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
return string.Empty;
string[] elementUri = value.ToString().Split('/');
bool isReSources = false;
StringBuilder resulf = new StringBuilder();
foreach (string s in elementUri)
{
if (s == "Resources")
{
isReSources = true;
}
if (isReSources)
{
resulf.Append(@"\" + s);
}
}
return resulf.ToString();
}
}
我认为您正在使其变得非常复杂。
[首先,在顶部窗口元素的Xaml中设置名称-例如CategoryAddFoodFormWindow:
<Window x:Class="CoffeeManager.Controls.CategoryControl.CategoryAddFoodForm" ... x:Name="CategoryAddFoodFormWindow">
然后,通过以下方法更轻松地绑定到CategoryAddFoodForm类中的属性:
<ImageBrush Stretch="Uniform" ImageSource="{Binding ElementName=CategoryAddFoodFormWindow, Path=Category.Image, Converter={StaticResource ConvertImage}, UpdateSourceTrigger=PropertyChanged}"/>
下一步,然后在类型为ImageSource的CategoryAddFoodForm上创建一个属性:>
public ImageSource CategoryImage { set; get; }
并将对话框中的值直接设置为该属性,而不使用转换器类:
<ImageBrush Stretch="Uniform" ImageSource="{Binding ElementName=CategoryAddFoodFormWindow, Path=CategoryImage/>
INotifyPropertyChanged是不必要的,并且类ConvertImage都是