想要在动态生成的布尔值列中显示 "Yes"/"No" 而不是 WPF 的 RadGridView 中的复选框

Want to display "Yes"/"No" in a dynamically generated column for Boolean value instead of checkbox in RadGridView for WPF

我想显示 "Yes" 或 "No" 每当收到布尔类型数据(它可以接收不同类型的数据)以在 RadGridView 中生成列而不是复选框。我想在 xaml 中实施此更改。列是动态生成的。现在是这样创建的:

 <telerik:RadGridView x:Name="Data" Grid.Row="3" Margin="5" AutoGenerateColumns="False" CanUserSortColumns="True" IsFilteringAllowed="True"
                                 grid:RadGridViewColumnsBinding.ColumnsCollection="{Binding Path=ColumnsData}"
                                 IsReadOnly="False" CanUserResizeColumns="True"/>

我是 Silverlight 编码新手。如果有人能提供帮助,将不胜感激。

如果您还没有,您应该查看他们在这个 demo, and read about IValueConverter 中的 Telerik 的 ConditionalDataTemplateSelector。

根据您尝试对所有列执行的操作,ConditionalDataTemplateSelector 可能有点矫枉过正,但您可以使用它创建一个规则系统,以根据自定义规则系统为给定单元格使用哪些 DataTemplate。

<Grid.Resources>
    ...
    <DataTemplate x:Key="CellDisplayTextBox">
        <TextBlock Text="{Binding Value, Converter={StaticResource BooleanToYesNoConverter}}" />
    </DataTemplate>

    <selector:ConditionalDataTemplateSelector x:Key="displaySelector" ConditionConverter="{StaticResource someConverter}">
        <selector:ConditionalDataTemplateSelector.Rules>
            <selector:ConditionalDataTemplateRule DataTemplate="{StaticResource CellDisplayTextBox}">
                <selector:ConditionalDataTemplateRule.Value>
                    <sys:Int32>1</sys:Int32> <!--You need to figure out what value and type to use here -->
                </selector:ConditionalDataTemplateRule.Value>
            </selector:ConditionalDataTemplateRule>
            ...
    </selector:ConditionalDataTemplateSelector.Rules>
</Grid.Resources>
...
<telerikGridView:RadGridView>
    <telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn CellTemplateSelector="{StaticResource displaySelector}" CellEditTemplateSelector="{StaticResource editSelector}" />
    </telerik:RadGridView.Columns>
</telerikGridView:RadGridView>

IValueConverter 可让您绑定布尔值,但显示字符串值。对于 BooleanToYesNoConverter,您可以执行以下操作:

public class BooleanToYesNoConverter: IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        bool? bValue = value as bool?;

        if (bValue.HasValue)
            return bValue.Value ? "Yes" : "No";
        else
            return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string sValue = value as string;
        return sValue == "Yes";
    }
}

演示中的 ConditionalDataTemplateSelector 代码:

public class ConditionalDataTemplateSelector : DataTemplateSelector
{
    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        object conditionValue = this.ConditionConverter.Convert(item, null, null, null);
        foreach (ConditionalDataTemplateRule rule in this.Rules)
        {
            if (Equals(rule.Value, conditionValue))
            {
                return rule.DataTemplate;
            }
        }

        return base.SelectTemplate(item, container);
    }

    List<ConditionalDataTemplateRule> _Rules;
    public List<ConditionalDataTemplateRule> Rules 
    {
        get
        {
            if (this._Rules == null)
            {
                this._Rules = new List<ConditionalDataTemplateRule>();
            }

            return this._Rules;
        }
    }

    IValueConverter _ConditionConverter;
    public IValueConverter ConditionConverter
    {
        get
        {
            return this._ConditionConverter;
        }
        set
        {
            this._ConditionConverter = value;
        }
    }
}

public class ConditionalDataTemplateRule
{
    object _Value;
    public object Value
    {
        get
        {
            return this._Value;
        }
        set
        {
            this._Value = value;
        }
    }

    DataTemplate _DataTemplate;
    public DataTemplate DataTemplate
    {
        get
        {
            return this._DataTemplate;
        }
        set
        {
            this._DataTemplate = value;
        }
    }
}