C# WPF MVVM 图像源更新

问题描述 投票:0回答:1

我正在尝试从视图模型更新 xml 中的图像源,但似乎我无法做到这一点,而且我不明白我缺少什么。

所以...在视图 xml 中,我有一个图像,我为模型中的字符串进行了绑定。

<Image x:Name="Pick1Image"
                               Grid.Column="0"
                               Grid.Row="7"
                               Grid.ColumnSpan="6"
                               Source="{Binding ImagePath}"
                               Style="{StaticResource Image_Viewer_Pick}"
                               />

然后,在我的视图模型中,我制作了这部分,其中检查变量的值并将路径字符串相应地分配给绑定变量。

public string SelectedPart
        {
            get { return _selectedPart; }
            set
            {
                _selectedPart = value;

                if(_selectedPart == "SX") {
                    _imagesource = "/Images/main_view.png";
                }

                if (_selectedPart == "DX")
                {
                    _imagesource = "/Images/second_view.png";
                }

                if (_selectedPart == "TX")
                {
                    _imagesource = "/Images/third_view.png";
                }

            }
        }

当然,我有通过 OnPropertyChanged() 调用进行绑定变量声明。

public string ImagePath
        {
            get { return _imagesource; }
            set { _imagesource = value; OnPropertyChanged(); }
        }

如果我使用 Messagebox.Show(ImagePath) 变量会正常更改,但图像保持不变。可能是什么问题?

谢谢!

c# wpf mvvm binding imagesource
1个回答
0
投票

您需要将 ImageSource/位图类型绑定到控件,或者,另一种方法尝试将您的图像路径与转换器绑定,该转换器将字符串类型图像路径绑定到 ImageSource。

© www.soinside.com 2019 - 2024. All rights reserved.