I have an Ellipse in my application which is filled with an ImageBrush. I'm trying to bind ImageBrush.ImageSource and change it when I click on the Ellipse. I'm using the class ConvertImage which implements IValueConverter to convert the data. But when I changed data and call function PropertiesChanged it not call class ConvertImage and not reminding data.
Here is my code 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>
and here is code behind:
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
}
}
}
}
final here is class 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();
}
}