C# 如何在代码中设置属性值?

C# 如何在代码中设置属性值?,c#,wpf,xaml,C#,Wpf,Xaml,如何在代码中设置样式的属性值?我有一个resourcedictionary,我想更改代码中的一些属性,我该怎么做 wpf代码: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Style x:Key=

如何在代码中设置样式的属性值?我有一个resourcedictionary,我想更改代码中的一些属性,我该怎么做

wpf代码:

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style
        x:Key="ButtonKeyStyle"
        TargetType="{x:Type Button}">

        <Setter Property="Width" Value="{Binding MyWidth}"/>
        ....

我做错了什么?

您没有解释在运行代码时发生了什么(或没有发生什么)。但是,您发布的代码正在创建一个新的Setter(…),但是没有显示您正在使用它做什么。您需要将创建的setter添加到样式中,以使其生效


但是,在您所引用的样式的Xaml中已经有一个宽度属性的setter。因此,我怀疑您实际上想要编辑现有的setter,而不是创建一个新的setter。

您没有解释在运行代码时发生了什么(或没有发生什么)。但是,您发布的代码正在创建一个新的Setter(…),但是没有显示您正在使用它做什么。您需要将创建的setter添加到样式中,以使其生效


但是,在您所引用的样式的Xaml中已经有一个宽度属性的setter。因此,我怀疑您实际上想要编辑现有的setter,而不是创建一个新的setter。

为什么不在XAML中创建按钮,然后实现
INotifyPropertyChanged
-接口并在代码中创建“MyWidth”的属性?它可能是这样的:

XAML:


Viewmodel/Codebehind:

// This is your private variable and its public property
private double _myWidth;
public double MyWidth
{
    get { return _myWidth; }
    set { SetField(ref _myWidth, value, "MyWidth"); } // You could use "set { _myWidth = value; RaisePropertyChanged("MyWidth"); }", but this is cleaner. See SetField<T>() method below.
}

// Feel free to add as much properties as you need and bind them. Examples:
private double _myHeight;
public double MyHeight
{
    get { return _myHeight; }
    set { SetField(ref _myHeight, value, "MyHeight"); }
}

private string _myText;
public double MyText
{
    get { return _myText; }
    set { SetField(ref _myText, value, "MyText"); }
}

// This is the implementation of INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(String propertyName)
{
    if (PropertyChanged != null)
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}

// Prevents your code from accidentially running into an infinite loop in certain cases
protected bool SetField<T>(ref T field, T value, string propertyName)
{
    if (EqualityComparer<T>.Default.Equals(field, value))
            return false;

    field = value;
    RaisePropertyChanged(propertyName);
    return true;
}
//这是您的私有变量及其公共属性
私人双_myWidth;
公共双MyWidth
{
获取{return\u myWidth;}
set{SetField(ref{u myWidth,value,“myWidth”);}//您可以使用“set{u myWidth=value;RaisePropertyChanged(“myWidth”);}”,但这更干净。请参阅下面的SetField()方法。
}
//您可以随意添加所需的属性并进行绑定。示例:
私人双倍身高;
公众双倍身高
{
获取{return\u myHeight;}
set{SetField(ref _myHeight,value,“myHeight”);}
}
私有字符串_myText;
公共双MyText
{
获取{return\u myText;}
set{SetField(ref _myText,value,“myText”);}
}
//这是INotifyPropertyChanged的实现
公共事件属性更改事件处理程序属性更改;
受保护的void RaisePropertyChanged(字符串propertyName)
{
if(PropertyChanged!=null)
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
//防止代码在某些情况下意外运行到无限循环中
受保护的布尔设置字段(参考T字段、T值、字符串属性名称)
{
if(EqualityComparer.Default.Equals(字段,值))
返回false;
字段=值;
RaisePropertyChanged(propertyName);
返回true;
}

然后,您可以将按钮的宽度属性绑定到这个“MyWidth”属性,它将在每次您在代码中设置“MyWidth”时自动更新。您需要设置属性,而不是私有变量本身。否则它不会触发更新事件,您的按钮也不会更改。

为什么不在XAML中创建按钮,然后实现
INotifyPropertyChanged
-接口,并在代码中创建“MyWidth”的属性?它可能是这样的:

XAML:


Viewmodel/Codebehind:

// This is your private variable and its public property
private double _myWidth;
public double MyWidth
{
    get { return _myWidth; }
    set { SetField(ref _myWidth, value, "MyWidth"); } // You could use "set { _myWidth = value; RaisePropertyChanged("MyWidth"); }", but this is cleaner. See SetField<T>() method below.
}

// Feel free to add as much properties as you need and bind them. Examples:
private double _myHeight;
public double MyHeight
{
    get { return _myHeight; }
    set { SetField(ref _myHeight, value, "MyHeight"); }
}

private string _myText;
public double MyText
{
    get { return _myText; }
    set { SetField(ref _myText, value, "MyText"); }
}

// This is the implementation of INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(String propertyName)
{
    if (PropertyChanged != null)
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}

// Prevents your code from accidentially running into an infinite loop in certain cases
protected bool SetField<T>(ref T field, T value, string propertyName)
{
    if (EqualityComparer<T>.Default.Equals(field, value))
            return false;

    field = value;
    RaisePropertyChanged(propertyName);
    return true;
}
//这是您的私有变量及其公共属性
私人双_myWidth;
公共双MyWidth
{
获取{return\u myWidth;}
set{SetField(ref{u myWidth,value,“myWidth”);}//您可以使用“set{u myWidth=value;RaisePropertyChanged(“myWidth”);}”,但这更干净。请参阅下面的SetField()方法。
}
//您可以随意添加所需的属性并进行绑定。示例:
私人双倍身高;
公众双倍身高
{
获取{return\u myHeight;}
set{SetField(ref _myHeight,value,“myHeight”);}
}
私有字符串_myText;
公共双MyText
{
获取{return\u myText;}
set{SetField(ref _myText,value,“myText”);}
}
//这是INotifyPropertyChanged的实现
公共事件属性更改事件处理程序属性更改;
受保护的void RaisePropertyChanged(字符串propertyName)
{
if(PropertyChanged!=null)
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
//防止代码在某些情况下意外运行到无限循环中
受保护的布尔设置字段(参考T字段、T值、字符串属性名称)
{
if(EqualityComparer.Default.Equals(字段,值))
返回false;
字段=值;
RaisePropertyChanged(propertyName);
返回true;
}

然后,您可以将按钮的宽度属性绑定到这个“MyWidth”属性,它将在每次您在代码中设置“MyWidth”时自动更新。您需要设置属性,而不是私有变量本身。否则它不会触发更新事件,您的按钮也不会更改。

Hmm,怎么了?您当前的实现有什么不满意的地方?嗯,怎么了?您当前的实现是什么,让您不满意?我只想更改width属性的值,在同一个ResourceDiciatory中,我有一个textblock并更改文本,所以执行:wpf代码:按照我所说的操作不会更改宽度并设置最大容器我只想更改width属性的值,在同一个资源词典中,我有一个文本块并更改文本,so do:wpf代码:按照我所说的操作不会更改宽度并设置最大容器,如果在应用程序中共享所讨论的样式,那么这将很难工作-您必须在应用程序中的每个窗口(或可能的UserControl)周围传递实现该属性的任何类的相同实例,如果所讨论的样式在整个应用程序中共享,那么这将不容易实现-您必须在应用程序中的每个窗口(或可能的UserControl)周围传递实现该属性的任何类的相同实例,或者将其作为静态公开。
// This is your private variable and its public property
private double _myWidth;
public double MyWidth
{
    get { return _myWidth; }
    set { SetField(ref _myWidth, value, "MyWidth"); } // You could use "set { _myWidth = value; RaisePropertyChanged("MyWidth"); }", but this is cleaner. See SetField<T>() method below.
}

// Feel free to add as much properties as you need and bind them. Examples:
private double _myHeight;
public double MyHeight
{
    get { return _myHeight; }
    set { SetField(ref _myHeight, value, "MyHeight"); }
}

private string _myText;
public double MyText
{
    get { return _myText; }
    set { SetField(ref _myText, value, "MyText"); }
}

// This is the implementation of INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(String propertyName)
{
    if (PropertyChanged != null)
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}

// Prevents your code from accidentially running into an infinite loop in certain cases
protected bool SetField<T>(ref T field, T value, string propertyName)
{
    if (EqualityComparer<T>.Default.Equals(field, value))
            return false;

    field = value;
    RaisePropertyChanged(propertyName);
    return true;
}