Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 定义stringformat时,TextBox未更新其视觉效果_C#_Wpf_Mvvm - Fatal编程技术网

C# 定义stringformat时,TextBox未更新其视觉效果

C# 定义stringformat时,TextBox未更新其视觉效果,c#,wpf,mvvm,C#,Wpf,Mvvm,我想在我的虚拟机上绑定一个文本框到一个doule。在XAML中,我有: <TextBox Grid.Row="2" Grid.Column="1" Margin="0,0,0,5"> <Binding Path="Offset" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay" StringFormat="{}{0:F2}}"> <Binding.Validatio

我想在我的虚拟机上绑定一个文本框到一个doule。在XAML中,我有:

<TextBox Grid.Row="2" Grid.Column="1"    Margin="0,0,0,5">
        <Binding Path="Offset" UpdateSourceTrigger="PropertyChanged"  Mode="TwoWay" StringFormat="{}{0:F2}}">
            <Binding.ValidationRules>
                <ExceptionValidationRule />
            </Binding.ValidationRules>
        </Binding>
    </TextBox>
  public double Offset
    {
        get
        {
            return this.offset;
        }

        set
        {
            if (value <= 0)
            {
                throw new Exception("Not Valid!");
            }

            this.offset = value;
            this.NotifyOfPropertyChange(() => this.Offset);
        }
    }

在虚拟机上,我有一个:

<TextBox Grid.Row="2" Grid.Column="1"    Margin="0,0,0,5">
        <Binding Path="Offset" UpdateSourceTrigger="PropertyChanged"  Mode="TwoWay" StringFormat="{}{0:F2}}">
            <Binding.ValidationRules>
                <ExceptionValidationRule />
            </Binding.ValidationRules>
        </Binding>
    </TextBox>
  public double Offset
    {
        get
        {
            return this.offset;
        }

        set
        {
            if (value <= 0)
            {
                throw new Exception("Not Valid!");
            }

            this.offset = value;
            this.NotifyOfPropertyChange(() => this.Offset);
        }
    }
公共双偏移
{
得到
{
返回此.offset;
}
设置
{
if(该偏移量的值);
}
}
但当我在vm上将一个值设置为Offset时,视图上什么也不显示

如果我删除字符串格式,我将在视图中获得合适的显示

  <TextBox Grid.Row="2" Grid.Column="1"    Margin="0,0,0,5">
        <Binding Path="Offset" UpdateSourceTrigger="PropertyChanged"  Mode="TwoWay" >
            <Binding.ValidationRules>
                <ExceptionValidationRule />
            </Binding.ValidationRules>
        </Binding>
    </TextBox>


如何在VM上同时使用stringformat和设置偏移量?

您得到了一个额外的
,最后,将其更改为:

StringFormat=“{}{0:F2}”


(这可能是从类似
的复制过程中遗留下来的)

我认为您最终得到了一个额外的
,应该是
StringFormat=“{}{0:F2}”
。你确定你在
Offset
属性中使用了
this.Offset
,还是这是一个打字错误?@Bolu:你说得对。现在修好了。非常感谢。请把它作为一个答案,这样我就可以接受了。因为这个问题是由一个简单的印刷错误引起的,不能再被复制了,所以我投票决定以“离题”结束它。随着时间的推移,其他评论者也将投票关闭它,因此它将被关闭。因为它对除你之外的任何人都没有帮助,如果你自己删除它(在你接受@Bolu的答案之后),你可以节省一些时间和复习。非常感谢。@Sheridan,我想说的是,由于其他用户复制此错误的可能性很高,我不能100%确定是否需要删除此错误。@Sheridan,我不担心声誉,我是在捍卫我回答的目的。。问自己一个问题:这篇文章(以及它的答案)是否会让其他人受益,或者它是如此特定于OP的情况,以至于只会让他受益?