C# 如何使用ViewModel添加XAML属性值?

C# 如何使用ViewModel添加XAML属性值?,c#,xaml,viewmodel,C#,Xaml,Viewmodel,我想在单击按钮后更改UI端的某些内容。比如说,单击按钮后更改按钮宽度 <Button x:Name="btnButt" Content="Button"/> 然而,一切都不起作用。我还尝试从ViewModel绑定字符串: \\ XAML <Button x:Name="btnButt" Content="Button" Width="{Binding WidthVM}"/>

我想在单击按钮后更改UI端的某些内容。比如说,单击按钮后更改按钮宽度

<Button x:Name="btnButt" Content="Button"/>
然而,一切都不起作用。我还尝试从ViewModel绑定字符串:

\\ XAML
<Button x:Name="btnButt" Content="Button" Width="{Binding WidthVM}"/>

\\ ViewModel
public int WidthVM = 3000;
\\XAML
\\视图模型
公共整数宽度vm=3000;

可能缺少什么?

我不会推荐您的第一个解决方案,因为它会破坏MVVM的用途

您的第二个解决方案实际上是正确的,即绑定来自VM的一些数据。它不起作用,因为您需要以MVVM的方式将信号更改发送到UI,如下所示:

public const string WidthVMPropertyName = "WidthVM";

private int _widthVM = 0;

public int WidthVM
{
    get
    {
        return _widthVM;
    }
    set
    {
        Set(WidthVMPropertyName, ref _widthVM, value);
    }
}

\\ then set it like:


WidthVM = 3000;

什么是查看.MyDialog?这是
ViewModel
类还是支持类?绑定时的ViewModel代码是什么?@cahyo,这是我的XAML类
public const string WidthVMPropertyName = "WidthVM";

private int _widthVM = 0;

public int WidthVM
{
    get
    {
        return _widthVM;
    }
    set
    {
        Set(WidthVMPropertyName, ref _widthVM, value);
    }
}

\\ then set it like:


WidthVM = 3000;