如何在WPF中在视图中更改属性时(图像标签中的源属性)更改属性?

huangapple go评论75阅读模式
英文:

How to change a property when an attribute changes in the view(Source attribute in image tag) in WPF?

问题

这段代码用于监测图像的源是否被用户更改,并在用户更改图像时自动在视图模型中更新图像路径。以下是这段代码的相关部分的翻译:

// 这段代码是图像源更改的地方。
OpenFileDialog fileDialog = new OpenFileDialog();
if (fileDialog.ShowDialog() == true)
{
    Image.Source = new BitmapImage(new Uri(fileDialog.FileName)); // 在这里我想要同时在我的视图模型中更改图像路径。
    isImageChanged = true;
}

XAML 代码:

<Image Width="50"
       Name="Image"
       Height="50"
       Source="{Binding ImagePath, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource StringToPathConverter}}">
</Image>

视图模型:

private string _imagePath;
public string ImagePath
{
    get
    {
        return _imagePath;
    }
    set
    {
        _imagePath = value;
        OnPropertyChanged(nameof(ImagePath)); // 断点位置
    }
}

这是转换器代码:

public class StringToPathConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string res = AppDomain.CurrentDomain.BaseDirectory.ToString() + "images\\" + value;
        return res;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string res = Path.GetFileName(value.ToString());
        return res;
    }
}

您提到在设置图像路径时设置了断点,但它没有起作用。这可能是由于数据绑定问题或其他原因引起的。确保视图模型实现了 INotifyPropertyChanged 接口,并且 OnPropertyChanged 方法正确地触发了属性更改事件。如果一切设置正确,绑定应该能够在图像源更改时更新视图模型中的图像路径。如果问题仍然存在,您可能需要检查数据绑定的其他设置或调试代码以找出问题所在。

英文:

I have an image and I want to get notified when the source of my image changes by the user. So when the user changes the image it should automatically change in the view model.

This code is where my image source changes.

OpenFileDialog fileDialog = new OpenFileDialog();
if (fileDialog.ShowDialog() == true)
{
		Image.Source = new BitmapImage(new Uri(fileDialog.FileName)); //here I want to 
		 //change the imagePath in my view model too.
		isImageChanged = true;
}

xaml code

&lt;Image Width=&quot;50&quot;
       Name = &quot;Image&quot;
       Height=&quot;50&quot;
       Source=&quot;{Binding ImagePath,UpdateSourceTrigger=PropertyChanged},Converter={StaticResource StringToPathConverter}&quot;&gt;
            
&lt;/Image&gt;

view model

private string _imagePath;
public string ImagePath
{
   get
   {
     return _imagePath;
   }
   set
   {
       _imagePath = value;
       OnPropertyChanged(nameof(ImagePath));//break point
   }
}

And here is the converter code

public class StringToPathConverter:IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string res = AppDomain.CurrentDomain.BaseDirectory.ToString() + &quot;images\\&quot; + value;
            return res;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {            
            string res = Path.GetFileName(value.ToString());
            return res;
        }
    }

I set a breakpoint on set of imagePath and it didn't work. I don't know if it is possible to do it with binding and if it is or it's not how?

答案1

得分: 2

Image.Source 属性的绑定默认是 OneWay

您必须将其声明为 TwoWay

<Image Source="{Binding ImagePath,
                    Mode=TwoWay,
                    Converter={StaticResource StringToPathConverter}}"/>

请注意,这需要您的 StringToPathConverter 实现 ConvertBack 方法,将 ImageSource 转换为路径字符串。

您应该避免这样做,而是直接更改源属性:

if (fileDialog.ShowDialog() == true)
{
    viewModel.ImagePath = fileDialog.FileName;
}
英文:

The Binding of the Image.Source property is OneWay by default.

You have to declare it as TwoWay:

&lt;Image Source=&quot;{Binding ImagePath,
                Mode=TwoWay,
                Converter={StaticResource StringToPathConverter}}&quot;/&gt;

Be aware that this requires a working implementation of the ConvertBack method of your StringToPathConverter, which would have to convert from ImageSource to a path string.

You should avoid that and instead change the source property directly:

if (fileDialog.ShowDialog() == true)
{
    viewModel.ImagePath = fileDialog.FileName;
}

huangapple
  • 本文由 发表于 2023年7月24日 17:40:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76753186.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定