4

DataGridオブジェクトを含む WPF アプリケーションにコントロールがあります。ユーザーの操作によって変更できる、そのオブジェクトのブール値のプロパティがあります。そのプロパティの値が変更されたときに行のスタイルを変更する必要があります。

から派生するクラスを作成しましたStyleSelector

public class LiveModeSelector : StyleSelector {

    public Style LiveModeStyle { get; set; }
    public Style NormalStyle { get; set; }

    public override Style SelectStyle( object item, DependencyObject container ) {
        DataGridRow gridRow = container as DataGridRow;
        LPRCamera camera = item as LPRCamera;
        if ( camera != null && camera.IsInLiveMode ) {
            return LiveModeStyle;
        }
        return NormalStyle;
    }
}

問題の View Model クラスは を実装しINotifyPropertyChangedPropertyChanged問題のプロパティが変更されたときにイベントを発生させます。

// Note:  The ModuleMonitor class implements INotifyPropertyChanged and raises the PropertyChanged
// event in the SetAndNotify generic method.
public class LPRCamera : ModuleMonitor, ICloneable {

    . . .

    public bool IsInLiveMode {
        get { return iIsInLiveMode; }
        private set { SetAndNotify( "IsInLiveMode", ref iIsInLiveMode, value ); }
    }
    private bool iIsInLiveMode;

    . . .

    /// </summary>
    public void StartLiveMode() {
        IsInLiveMode = true;

         . . .
    }


    public void StopLiveMode() {
        IsInLiveMode = false;

        . . .
    }
}

ユーザーが必要なアクションを実行すると、プロパティの値が変更されますが、スタイルは変更されません。

SelectStyle メソッドにブレークポイントを配置しましたが、コントロールが最初に読み込まれたときにブレークポイントがヒットすることがわかりますが、プロパティの値が変更されたときにはヒットしません。

私は何が欠けていますか?

4

2 に答える 2

5

私の質問に対する@Rachelの回答から生まれたこれを行う方法を見つけました。ただし、コードの詳細は多少異なるため、何が機能するかを正確に示したいと思います。

最初のステップは、クラスの 2 つの異なるものStylesを 1つに結合することでした。DataGridRow

<Style TargetType="DataGridRow" x:Key="CameraStyle">
    <Setter Property="Foreground" Value="{DynamicResource TextForeground}" />
    <Setter Property="Background" Value="{DynamicResource DataBackground}" />
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=IsInLiveMode}" Value="True">
            <Setter Property="Foreground" Value="Red" />
            <Setter Property="Background" Value="Yellow" />
        </DataTrigger>
    </Style.Triggers>
</Style>

2 番目のステップは、DataGridコントロールのRowStyleプロパティをこの新しいスタイルに設定することでした。

<DataGrid . . .
          RowStyle={StaticResource CameraStyle}">
          . . .
</DataGrid>

これは機能します。ユーザーがその行に関連付けられている行をライブ モードにすると、行の前景と背景が変化し、LPRCameraライブ モードから外すと通常に戻ります。

ありがとう@レイチェル!

于 2012-08-27T21:38:28.897 に答える
4

StyleSelectorは通知をリッスンしないと思うので、プロパティが変更されPropertyChangeても再実行されません。IsInLiveMode

DataTrigger代わりにベースにスタイルを配置するIsInLiveModeと、プロパティの変更通知が発生するたびに再評価されます。

<DataGrid.Resources>
    <Style TargetType="{x:Type DataGridRow}" x:Key="Style1">
        <Setter Property="Background" Value="Red" />
    </Style>
    <Style TargetType="{x:Type DataGridRow}" x:Key="Style2">
        <Setter Property="Background" Value="Blue" />
    </Style>
</DataGrid.Resources>

<DataGrid.Style>
    <Style TargetType="{x:Type DataGrid}">
        <Setter Property="RowStyle" Value="{StaticResource Style1}" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding ElementName=MyDataGrid, Path=DataContext.IsInLiveMode}" Value="True">
                <Setter Property="RowStyle" Value="{StaticResource Style2}" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</DataGrid.Style>
于 2012-08-27T19:21:04.820 に答える