3

データグリッド セル (チェックボックス、タイムピッカー、コンボボックスなど) が無効に設定されている場合に、灰色がフェードアウトする動作を取り除きたいです。

ここに画像の説明を入力

だから、これは私がやろうとしていることです:

<Style TargetType="DataGridCell">
    <Style.Triggers>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Foreground" Value="{x:Null}" />
            <Setter Property="Background" Value="{x:Null}" />
            <Setter Property="BorderBrush" Value="{x:Null}" />
        </Trigger>
    </Style.Triggers>
</Style>

しかし、うまくいきません。

実際に datagridcell 内にあるコントロール (チェックボックス、コンボボックスなど) のスタイルを個別に定義する必要がありますか? この作業を行う良い方法は何ですか?

4

1 に答える 1

1

まず、特定のコントロールでプロパティがハードコードされているため、色がコントロールでハードコードされているため、プロパティを設定するIsEnabled ことBackgroundは不可能です。例 - Backgroundin: ComboBoxTextBoxなど したがって、そのような場合overridingは、コントロールのデフォルトの動作 (この場合はIsEnabled=False動作) であるスタイルとテンプレートを作成します。

次に、割り当てられたプロパティDataTemplate IsEnabledの次のようなコントロール:

<DataGridTemplateColumn x:Name="ComboBoxColumn" Header="ComboBox Header" Width="110">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ComboBox IsEnabled="False" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

DataGridCellとは言わないFalseので、トリガーは起動しません。

<Style.Triggers>
    <Trigger Property="IsEnabled" Value="False">
        <Setter Property="Foreground" Value="Black" />
    </Trigger>
</Style.Triggers>

したがって、結論:

コントロールが有効な場合の動作を決定しますfalse。各コントロールのスタイルは から取得できますMSDN

CheckBox(リンク)のスタイルの例:

<Style x:Key="{x:Type CheckBox}" TargetType="CheckBox">
    <Setter Property="SnapsToDevicePixels" Value="true"/>
    <Setter Property="OverridesDefaultStyle" Value="true"/>
    <Setter Property="FocusVisualStyle" Value="{StaticResource CheckBoxFocusVisual}"/>

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="CheckBox">
                <BulletDecorator Background="Transparent">
                    <BulletDecorator.Bullet>
                        <Border x:Name="Border" Width="13" Height="13" CornerRadius="0" Background="{StaticResource NormalBrush}" BorderThickness="1" BorderBrush="{StaticResource NormalBorderBrush}">
                            <Path Width="7" Height="7" x:Name="CheckMark" SnapsToDevicePixels="False" Stroke="{StaticResource GlyphBrush}" StrokeThickness="2" Data="M 0 0 L 7 7 M 0 7 L 7 0" />
                        </Border>
                    </BulletDecorator.Bullet>

                    <ContentPresenter Margin="4,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Left" RecognizesAccessKey="True" />
                </BulletDecorator>

                <ControlTemplate.Triggers>
                    <Trigger Property="IsChecked" Value="false">
                        <Setter TargetName="CheckMark" Property="Visibility" Value="Collapsed"/>
                    </Trigger>

                    <Trigger Property="IsChecked" Value="{x:Null}">
                        <Setter TargetName="CheckMark" Property="Data" Value="M 0 7 L 7 0" />
                    </Trigger>

                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter TargetName="Border" Property="Background" Value="{StaticResource DarkBrush}" />
                    </Trigger>

                    <Trigger Property="IsPressed" Value="true">
                        <Setter TargetName="Border" Property="Background" Value="{StaticResource PressedBrush}" />
                        <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource PressedBorderBrush}" />
                    </Trigger>                           

                    <!-- Here set the some properties there IsEnabled will be false -->
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter TargetName="Border" Property="Background" Value="Red" />
                        <Setter TargetName="Border" Property="BorderBrush" Value="Green" />
                        <Setter Property="Foreground" Value="Orange"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>               

DependencyPropertyオフになっているかどうかを示す列の添付を定義しました。その後、テンプレート列の各コントロールは次のように参照されます。

<DataGridTemplateColumn x:Name="CheckBoxColumn" local:MyDependencyClass.IsEnabledColumn="False" Width="110" Header="CheckBox Header">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <CheckBox Content="My CheckBox" IsEnabled="{Binding Source={x:Reference Name=CheckBoxColumn}, Path=(local:MyDependencyClass.IsEnabledColumn)}" IsChecked="False" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

プロパティIsEnabledColumnを設定します。無効にするコントロールに対して設定できます ( IsEnabled=False)。

のリストMyDependencyClass:

public class MyDependencyClass : DependencyObject
{
    public static readonly DependencyProperty IsEnabledColumnProperty;

    public static void SetIsEnabledColumn(DependencyObject DepObject, bool value)
    {
        DepObject.SetValue(IsEnabledColumnProperty, value);
    }

    public static bool GetIsEnabledColumn(DependencyObject DepObject)
    {
        return (bool)DepObject.GetValue(IsEnabledColumnProperty);
    }

    static MyDependencyClass()
    {
        PropertyMetadata MyPropertyMetadata = new PropertyMetadata(false);

        IsEnabledColumnProperty = DependencyProperty.RegisterAttached("IsEnabledColumn",
                                                            typeof(bool),
                                                            typeof(MyDependencyClass),
                                                            MyPropertyMetadata);
    }        
}

{x:Reference}PS警告メッセージは気にしないでください。無視してかまいません。

于 2013-07-14T17:09:55.170 に答える