简体   繁体   English

如果数字为正数或负数,则DataGridCell会更改

[英]DataGridCell change if number positive or negative

i have a DataGridTextColumn that accepts numbers from a database table called Numbers. 我有一个DataGridTextColumn,它接受来自称为Numbers的数据库表中的数字。 If the number in cell is positive than i want the next DataGridTextColumn to have the word "Positive" inside. 如果单元格中的数字为正,那么我希望下一个DataGridTextColumn的内部具有单词“ Positive”。 I tried with IConverter Styles but still dont know how to do it right.I hope someone can help me. 我尝试使用IConverter Styles,但仍然不知道如何正确操作。希望有人能帮助我。

This is my code so far: 到目前为止,这是我的代码:

<DataGrid Grid.Row="0" Grid.Column="1" AutoGenerateColumns="False" IsReadOnly="false" CanUserAddRows="True" Margin="5,5,10,5" ItemsSource="{Binding ElementName=Numbers}">

        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Num}" Header="Number" FontSize="16"/>

            <DataGridTextColumn Header="Positive/Negative" FontSize="16">

                <DataGridTextColumn.ElementStyle>
                    <Style TargetType="{x:Type TextBlock}">
                        <Setter Property="Text" Value="{Binding Amount, Converter={StaticResource PositiveNegativeConverter}}"/>
                    </Style>
                </DataGridTextColumn.ElementStyle>
            </DataGridTextColumn>
    </DataGrid>

I also have this inside Resources: 我在参考资料中也有这个:

    <local:PositiveNegativeConverter x:Key="PositiveNegativeConverter"/>
</UserControl.Resources>      

My Converter looks like this: 我的转换器看起来像这样:

class PositiveNegativeConverter : IValueConverter
{
  public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        bool revert = (parameter as string).StartsWith("-");

        string stringValue = value as string;
        string compareValue = parameter as string;

        if (revert)
        {
            return "Negative";
        }
        else
            return "Positive";
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

You do not have to add a style for that, just add the converter to the Binding in the DataGridTextColumn. 您不必为此添加样式,只需将转换器添加到DataGridTextColumn中的Binding中即可。

Something like this: 像这样:

<DataGridTextColumn Binding="{Binding Num, Converter={StaticResource PositiveNegativeConverter}}" Header="Number" FontSize="16"/>

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

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