Two Way Data Binding With a Object in WPF,Image Control
- by Candy
Sorry, my English is not very good,
I have a object "Stuffs"
"Stuffs" have a Property “Icon”
now:
xaml
<Button Click="Button_Click"><Image Width="80" Height="80" Source="{Binding Path=Icon,Converter={StaticResource ImageConverter}}"/></Button>
cs
private void Button_Click(object sender, RoutedEventArgs e)
{
IconFloder.Title = "Icon";
String IconFloderPath = AppDomain.CurrentDomain.BaseDirectory + ItemIconFloder;
if (!System.IO.Directory.Exists(IconFloderPath)) System.IO.Directory.CreateDirectory(IconFloderPath);
IconFloder.InitialDirectory = IconFloderPath;
IconFloder.Filter = "Image File|*.jpeg";
IconFloder.ValidateNames = true;
IconFloder.CheckPathExists = true;
IconFloder.CheckFileExists = true;
if (IconFloder.ShowDialog() == true)
{
HideImage.Text = ItemIconFloder + "\\" + IconFloder.SafeFileName;
((sender as Button).Content as Image).Source = new ImageConverter().Convert(ItemIconFloder + "\\" + IconFloder.SafeFileName, Type.GetType("System.Windows.Media.ImageSource"), null, new System.Globalization.CultureInfo("en-US")) as ImageSource;
}
}
class ImageConverter:IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is string&&!String.IsNullOrEmpty(value.ToString()))
{
try
{
return new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + value));
}
catch { }
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
I would like to click buttons, change the picture,
Also change Data Binding
Stuffs.Icon
But failed,I have no idea?I need help?
I do not know whether I speak clearly