Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.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# 文本框:绑定到double,通过IMultiValueConverter转换:在ViewModel中更改值时不更新_C#_Wpf_Formatting_Caliburn.micro_Imultivalueconverter - Fatal编程技术网

C# 文本框:绑定到double,通过IMultiValueConverter转换:在ViewModel中更改值时不更新

C# 文本框:绑定到double,通过IMultiValueConverter转换:在ViewModel中更改值时不更新,c#,wpf,formatting,caliburn.micro,imultivalueconverter,C#,Wpf,Formatting,Caliburn.micro,Imultivalueconverter,我用的是Caliburn.Micro 在我的视图中,一个文本框绑定到双X和一个按钮,它改变了ViewModel中X的值 public void ButtonPressed() { X = AnObject.GetDouble; NotifyOfPropertyChange(() => X); } public double X { get; set; } 我的目标是限制显示的小数位数。此数字可在应用程序中配置,因此可作为对象的属性

我用的是Caliburn.Micro

在我的视图中,一个
文本框
绑定到
双X
和一个
按钮
,它改变了ViewModel中X的值

public void ButtonPressed()
    {
        X = AnObject.GetDouble;
        NotifyOfPropertyChange(() => X);
    }

    public double X { get; set; }
我的目标是限制显示的小数位数。此数字可在应用程序中配置,因此可作为对象的属性使用。因此,我定义了一个
IMultiValueConverter

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        return String.Format(values[1].ToString(), values[0]);
    }
并将我的文本框定义如下:

<TextBox>
     <TextBox.Text>
           <MultiBinding Converter="{StaticResource coordinateConverter}" >
                 <Binding Path="X" />
                 <Binding Path="AnObject.FormatNumberOfDecimals" />
           </MultiBinding>
     </TextBox.Text>
</TextBox>

工作原理:X的初始值零的格式正确

什么不起作用:按下按钮时,文本框中不会显示新值


不使用IMultiValueConverter的建议也很受欢迎。

结果是我的
字符串格式numberofdecimals
应该是
{0:0.0000}
格式,而不是
0.00000

0.0000
适用于标签的ContentStringFormat,在我的Visual Studio Designer中不会导致异常,但在使用
String.Format()
时,显然不适用于文本框

{0:0.0000}
使用
String.Format()
处理标签的ContentStringFormat和我的文本框。不幸的是,我的VisualStudio设计视图现在抛出了一个异常,对于我的文本框视图没有任何用处

System.FormatException
Input string was not in a correct format.
at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args)
at System.String.Format(IFormatProvider provider, String format, Object[] args)
at System.String.Format(String format, Object arg0)

我过去也遇到过类似的问题,当一个绑定值更新时,
MultiConverter
不会自动更新

由于第二个值未使用绑定,因此可以切换到单个
IValueConverter
,并将格式作为
ConverterParameter

或者简单地使用绑定的
StringFormat
属性

<TextBox Text="{Binding X, StringFormat=D4}" />

请注意,如果要绑定
内容
属性而不是
文本
属性,例如在
标签
上,则绑定的
StringFormat
属性将不起作用,您需要改为设置标签的
ContentStringFormat
属性。

有效

十进制借方=0; 十进制信用=0

        decimal.TryParse(values[0].ToString(), out debit);
        decimal.TryParse(values[1].ToString(), out credit);

        decimal total = debit - credit;

        return total.ToString("0.00"); // string form 

尝试将绑定模式设置为双向。是否以另一种方式工作?在类名后面是否有
INotifyPropertyChanged
?请检查ContentStringFormat和StringFormat之间的区别-有一些stackoverflow帖子指出我的格式是动态更改的,因此我需要将绑定传递给ConverterParameter,我觉得这是不可能的。