簡體   English   中英

使用 C# WPF DataGrid DataGridTemplateColumn 時將行而不是單元格綁定到轉換器

[英]Bind row instead of cell to converter when using C# WPF DataGrid DataGridTemplateColumn

我有一個動態設置 ItemsSource 的數據網格,我使用 DataGridTemplateColumns 來確定可用的下拉選項以及顯示驗證。 在驗證的情況下,我將單元格背景着色為紅色,以便對用戶顯而易見。 我正在使用轉換器來執行此操作:

public class ExampleForSOBrushConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string input = value as string;
        switch (input)
        {
            case "0":
            case "":
            case null:
                return Brushes.Red;                    
            default:
                return DependencyProperty.UnsetValue;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

樣式代碼如下所示:

<DataGridTemplateColumn.CellStyle>
                                    <Style TargetType="{x:Type DataGridCell}">
                                        <Setter Property="Background" Value="{Binding NameOfColumnInDataRow, Converter={StaticResource ExampleForSOBrushConverter}}"/>
                                    </Style>
                                </DataGridTemplateColumn.CellStyle>

所有這些都是檢查單元格上輸入的值是否為空白、空或包含 0(未選中的默認下拉選項)。 到目前為止,這工作得很好,但是,我現在需要基於其他列或通過不同的檢查(例如文本長度)對某些列應用驗證。 例如,如果 ColumnB 是 1,而 ColumnC 是 3,或者如果 ColumnD 少於 8 個字符。

我的計划是使用參數並像這樣傳遞行:

<DataGridTemplateColumn.CellStyle>
                                    <Style TargetType="{x:Type DataGridRow}">
                                        <Setter Property="Background" Value="{Binding Path=., Converter={StaticResource MultipleValidatorConverter}, ConverterParameter=LAR}"/>
                                    </Style>
                                </DataGridTemplateColumn.CellStyle>

然后轉換器看起來像這樣:

public class MultipleValidatorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        classNameUsedForItemsSource row = value as classNameUsedForItemsSource ;
        string param = parameter as string;

        if(param == "LAR")
        {
            if (param.Length < 8) return Brushes.Red;
        }
        // Other checks here based on parameter

        return DependencyProperty.UnsetValue;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

但遺憾的是,這會引發錯誤,指出 DataGridRow 與目標類型 DataGridCell 不匹配,現在我被卡住了。 在谷歌和這里查看,但無法找到符合我要求的匹配項。

我不相信您可以將列綁定到轉換器的參數,所以我不能追求這一點,我真的需要傳遞整行,以便我可以檢查其他相關字段或將多個綁定傳遞給轉換器。

盡管有有用的評論,但在驗證工作時,它在編輯后並沒有刷新單元格樣式。 因此我改變了我的方法並使用以下解決方案而不是使用多值轉換器。

public class IlrMultipleValidatorConverter : IMultiValueConverter
{
    public object Convert(object[] value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string param = parameter as string;

        if (param == "LAR")
        {
            string lar = value[0] as string;
            if (lar == null || lar == "") return Brushes.Red;
            else
            {
                if (lar.Length > 8) return Brushes.Red;
            }
        }
        if (param == "PwayCode")
        {
            string pway = value[0] as string;
            string ptype = value[1] as string;

            if (pway == null || pway == "")
            {
                if (ptype == "2" || ptype == "3" || ptype == "10" || ptype == "20" || ptype == "21" || ptype == "22" || ptype == "23") return Brushes.Red;
            }
        }


        return DependencyProperty.UnsetValue;
    }

    public object[] ConvertBack(object value, Type[] targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

這讓我可以在使用相同網格時添加更多條件,並像這樣設置每個樣式進行驗證:

<DataGridTemplateColumn.CellStyle>
    <Style TargetType="{x:Type DataGridCell}">
        <Setter Property="Background">
           <Setter.Value>
              <MultiBinding>
                  <MultiBinding.Converter>
                       <local:IlrMultipleValidatorConverter />
                  </MultiBinding.Converter>
                  <MultiBinding.ConverterParameter>PwayCode</MultiBinding.ConverterParameter>
                  <Binding Path="ApprenticeshipPathway" />
                  <Binding Path="ProgrammeType" />
              </MultiBinding>
           </Setter.Value>
       </Setter>
    </Style>
</DataGridTemplateColumn.CellStyle>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM