在WPF MVVM中,如何在运行时从文本框值调整图像大小?

时间:2018-08-02 14:23:05

标签: c# wpf mvvm

这里我有两个文本框名称LabelHeight和LabelWidth和一个图像。我从给labelheight和labelwidth指定的默认值加载图像。现在我想在运行时更改那些文本框值时更改图像高度和宽度。我是WPF的新手。 这是我的MainViewModel类,其中我声明了两个带有值的属性

 public int LabelWidth { get; set; } = 305;
    public int LabelHeight { get; set; } = 200;

这是我的xml

<Image x:Name="Image" 
           Grid.Column="2" Grid.Row="1"
               Source="Images/Norsel.bmp" 
               Height="{Binding LabelHeight}"  
               Width="{Binding LabelWidth}"
           VerticalAlignment="Top"/>
<TextBox  Grid.Column="1" Grid.Row="0" Margin="5" Text="{Binding LabelWidth, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
<TextBox  Grid.Row="1" Grid.Column="1" Margin="5" Text="{Binding LabelHeight, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>

1 个答案:

答案 0 :(得分:3)

您需要提高INotifyPropertyChanged的高度和宽度。

假设INotifyProperChanged在您的视图模型上已正确设置,则宽度类似这样

private int _labelWidth = 305;
public int LabelWidth 
{ 
     get { return _labelWidth; }  
     set 
     { 
         _labelWidth = value;
         NotifyPropertyChanged("LabelWidth");
     }
}