WPF - 文本框绑定十进制值

时间:2015-09-01 11:11:23

标签: wpf xaml data-binding textbox string-formatting

我正在用c#编写一个WPF程序,我试图找到一种方法来绑定两个带有十进制值的文本框。我有两个不同的文本框绑定两个不同的属性。

我希望当用户更改"成本" "价格"将自动填写,当他将改变" Price"费用也会自动填写。这产生了一个我不想要的循环。 而且我注意到带有小数值的文本框绑定不允许添加逗号和点字符'.'','

我如何解决这两个问题?

这些是文本框的XAML:

<TextBlock Grid.Row="0" Grid.Column="0" Text="Cost" Margin="5,5,0,0" />
<TextBox Grid.Row="1" Grid.Column="0"   Text="{Binding Cost, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<TextBlock Grid.Row="0" Grid.Column="1" Text="Price" Margin="5,5,0,0" />
<TextBox Grid.Row="1" Grid.Column="1"   Text="{Binding Price ,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

这些是两个属性:

private decimal _cost;
public decimal Cost
{
    get { return _cost; }
    set
    {
        if (_cost != value)
        {
            _cost = value;
            NotifyOfPropertyChange("Cost");

            if (Cost > 0)
            {
                Price = Math.Round(_cost * ((decimal)1.50), 2);
                NotifyOfPropertyChange("Price");
            }

        }
    }
}

private decimal _price;
public decimal Price
{
    get { return _price; }
    set
    {
        if (_price != value)
        {
            _price = value;
            NotifyOfPropertyChange("Price");

            if (Price > 0)
            {
                Cost = Math.Round(Price / (decimal)(1.55), 2);
                NotifyOfPropertyChange("Cost");
            }                    
        }
    }
}

编辑解决方案 在网上搜索我发现添加StringFormat=0{0.0}只需要解决方案here。希望它会帮助别人。

1 个答案:

答案 0 :(得分:1)

您可以使用后场来设置值。看到流动的

SELECT t1.id as id, t1.name, t1.email, t2a.profile_value as class, t2b.profile_value as phone
FROM Table1 as t1
LEFT JOIN Table2 t2a ON t2a.user_id = t1.id AND t2a.profile_key = 'profile.klasse'
LEFT JOIN Table2 t2b ON t2b.user_id = t1.id AND t2b.profile_key = 'profile.phone'