0

我对 WPF 相当陌生,我正在尝试创建一个自定义控件,将两个控件组合在一起,一个标签和一个文本框。自定义控件有两个依赖属性:

参数输入.cs

public static readonly DependencyProperty InputNameProperty = DependencyProperty.Register(nameof(InputName), typeof(string), typeof(ParameterInput), new PropertyMetadata(""));
public static readonly DependencyProperty InputProperty = DependencyProperty.Register(nameof(Input), typeof(string), typeof(ParameterInput), new PropertyMetadata(""));

public string InputName
{
   get{return (string)GetValue(InputNameProperty);}
   set{ SetValue(InputNameProperty, value);}
}
public string Input
{
   get{return (string)GetValue(InputProperty);}
   set{ SetValue(InputProperty, value);}
}

ParameterInputStyle.xaml这个在资源字典中设置如下

<ControlTemplate x:Key="ParameterInputStyleTemplate" TargetType="{x:Type local:ParameterInput}">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="135"/>
            <ColumnDefinition Width="100"/>
        </Grid.ColumnDefinitions>
        <Label Grid.Column="0" Style="{DynamicResource Label}" Content="{TemplateBinding InputName}"/>
        <TextBox Grid.Column="1" Style="{DynamicResource InputTextBox}" Text="{TemplateBinding Input}"/>
    </Grid>
</ControlTemplate> 

<Style x:Key="ParameterInputStyle" TargetType="{x:Type local:ParameterInput}">
    <Setter Property="Template" Value="{StaticResource ParameterInputStyleTemplate}"/>
</Style>

DataAccessVM.cs然后,在我的 ViewModel 中,我有几个属性,(注意,这不是静态类,但包含静态属性):

private static ImperialMeasurement _wallHeight { get; set; }
public static ImperialMeasurement wallHeight { get { return _wallHeight; } set { _wallHeight = value; } }

ImperialMeasurement.cs包含英尺、英寸的属性、numericValue(表示为英尺和英寸数字的双倍)和 stringFormat(它还实现 INotifyPropertyChanged),它所做的只是在字符串和数值之间进行转换:

    public string stringFormat
    {
        get
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(this._feet);
            sb.Append("\' ");
            sb.Append(this._inches);
            sb.Append("\"");
            return sb.ToString();
        }
        set
        {
            ImperialMeasurement imperialMeasurement = InputParser.TryReadImperialInput(value);

            if (imperialMeasurement != null)
            {
                this._feet = imperialMeasurement.feet;
                this._inches = imperialMeasurement.inches;
            }
            else
            {
                this._feet = 0;
                this._inches = 0;
            }
            OnPropertyChanged("stringFormat");
        }
    }

我尝试的一件事是在 DataAccessVM 中实现一个 StaticPropertyChanged,如下所示,但没有成功

private static ImperialMeasurement _wallHeight { get; set; }
public static ImperialMeasurement wallHeight { get { return _wallHeight; } set { _wallHeight = value; NotifyStaticPropertyChanged("wallHeight");} }

我正在尝试绑定到静态 ImperialMeasurement 类的字符串格式,它在 xaml 中的路径将是 backend:DataAccessVM.wallHeight.stringFormat。我已经读到您应该将路径括在括号中,如下所示:

<local:ParameterInput Input="{Binding Path=(backend:DataAccessVM.wallHeight.stringFormat)}"/>

然而,PropertyPath 不仅不允许您到达层次结构中的这一层,而且我相信这不适用于静态属性。我也试过

{Binding Path=(backend:DataAccessVM.wallHeight).stringFormat}"

{Binding Source={StaticResource DataAccess}, Path= wallHeight.stringFormat}"

任何帮助将非常感激!

4

0 回答 0