简体   繁体   English

将值和货币绑定到DataGridTextColumn

[英]Binding Value and Currency to DataGridTextColumn

I would like to bind currency for text column and if i type the value like "de" it is working and i can see Euro. 我想将货币绑定到文本列,如果我输入“ de”之类的值,则它可以正常工作,并且可以看到欧元。 But if I try to bind it it's sending an error. 但是,如果我尝试绑定它,则会发送错误。

Ofc i can bind the currency to whole View, but i have always 2 different currencies to display. 我可以将货币绑定到整个视图,但是我总是要显示2种不同的货币。

There is code that is not working: 有些代码不起作用:

Binding="{Binding Income,StringFormat=C,ConverterCulture={Binding CultureFormat}}" />

and If i do like this it's working properly: 如果我喜欢的话,它可以正常工作:

<DataGridTextColumn Header="Saldo" Binding="{Binding Balance,StringFormat=C,ConverterCulture=de}" />

I found that the is some solution to use multibinding, but i don't know how in this example. 我发现这是使用多重绑定的一些解决方案,但在此示例中我不知道如何。

Here is how you could to this using a multi binding and a converter: 这是使用多重绑定和转换器的方法:

public class CultureConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if(values[0] != DependencyProperty.UnsetValue && values[1] != DependencyProperty.UnsetValue)
        {
            decimal balance = System.Convert.ToDecimal(values[0]);
            string c = values[1] as string;

            return balance.ToString("C", CultureInfo.GetCultureInfo(c));
        }

        return Binding.DoNothing;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

XAML: XAML:

<DataGrid ...>
    <DataGrid.Resources>
        <local:CultureConverter x:Key="conv" />
    </DataGrid.Resources>
    <DataGrid.Columns>
        <DataGridTextColumn Header="Saldo">
            <DataGridTextColumn.Binding>
                <MultiBinding Converter="{StaticResource conv}">
                    <Binding Path="Income" />
                    <Binding Path="CultureFormat" />
                </MultiBinding>
            </DataGridTextColumn.Binding>
        </DataGridTextColumn>
       ...
    </DataGrid.Columns>
</DataGrid>

The example assumes that the CultureFormat property returns a string. 该示例假定CultureFormat属性返回一个字符串。 If it returns a CultureInfo you could just cast values[1] to this type instead of string . 如果返回的是CultureInfo ,则可以将values[1] CultureInfo为这种类型,而不是string

Binding is not a dependecy object and its properties are not dependency one. Binding不是依赖对象,其属性也不是依赖对象。 To apply a binding to Binding.ConverterCulture the source should be a dependency property and I guess that CultureFormat is not. 要将绑定应用于Binding.ConverterCulture ,源应该是依赖项属性,我想不是CultureFormat

If you need a variable culture info, you can format your data in the ViewModel using a specific property, something like 如果您需要可变的区域性信息,则可以使用特定属性在ViewModel中设置数据格式,例如

public string BalanceString
{
    get { return String.Format(CultureFormat, "C", Balance); }
}

and then use this new property for binding: 然后使用此新属性进行绑定:

<DataGridTextColumn Header="Saldo" Binding="{Binding BalanceString, Mode=OneWay}" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM