質問

レコードが変更されたときに、私のListViewに古い値と新しい値を表示する必要があります。私は、各セルが新しい価値と古い価値を示す必要があることを意味します。今のところ私はそうしています:

<GridViewColumn.CellTemplate>
    <DataTemplate>
         <StackPanel>
              <TextBlock Text="{Binding MyValue}"/>
              <TextBlock Margin="7,0,0,0" Text="{Binding old.MyValue}"/>
         </StackPanel>
    </DataTemplate>
</GridViewColumn.CellTemplate> 

次の列の場合、それは次のとおりです。

<GridViewColumn.CellTemplate>
    <DataTemplate>
         <StackPanel>
              <TextBlock Text="{Binding MySecondValue}"/>
              <TextBlock Margin="7,0,0,0" Text="{Binding old.MySecondValue}"/>
         </StackPanel>
    </DataTemplate>
</GridViewColumn.CellTemplate>

しかし、私は10列を持っていますが、これは10列すべてに多くのコピーパステを作ることはそれほど面白くありません。

これをよりコンパクトでより良くする方法はありますか?

私が欲しい理想的なバリアントは次のようなものです:

<GridViewColumn.CellTemplate>
    <DataTemplate>
         <MySpecialWhatever NewValueText="{Binding MyValue}" OldValueText="{Binding old.MyValue}" > 
         </MySpecialWhatever>
    </DataTemplate>
</GridViewColumn.CellTemplate>
役に立ちましたか?

解決

このようなカスタムUserControlで目標を達成できます。

doublevaluescell.xaml

<UserControl x:Class="WpfApplication1.DoubleValuesCell"
             x:Name="root"
             ...>
    <StackPanel>
        <TextBlock Text="{Binding NewValue, ElementName=root}"/>
        <TextBlock Margin="7,0,0,0" Text="{Binding OldValue, ElementName=root}"/>
    </StackPanel>
</UserControl>

doublevaluescell.xaml.cs

public partial class DoubleValuesCell : UserControl
{
    public static readonly DependencyProperty NewValueProperty = DependencyProperty.Register("NewValue", typeof(object), typeof(DoubleValuesCell));

    public static readonly DependencyProperty OldValueProperty = DependencyProperty.Register("OldValue", typeof(object), typeof(DoubleValuesCell));

    public object NewValue
    {
        get { return GetValue(NewValueProperty); }
        set { SetValue(NewValueProperty, value); }
    }

    public object OldValue
    {
        get { return GetValue(OldValueProperty); }
        set { SetValue(OldValueProperty, value); }
    }

    public DoubleValuesCell()
    {
        InitializeComponent();
    }
}

xxxwindow.xaml

<Window x:Class="WpfApplication1.XXXWindow"
        xmlns:local="clr-namespace:WpfApplication1"
        ...>
    ...
        <GridViewColumn>
            <GridViewColumn.CellTemplate>
                <DataTemplate>
                    <local:DoubleValuesCell NewValue="{Binding MyValue}" OldValue="{Binding old.MyValue}"/>
                </DataTemplate>
            </GridViewColumn.CellTemplate>
        </GridViewColumn>
    ...
</Window>

アップデート

DataTriggerで2番目のコントロールを崩壊させることができます。

<StackPanel>
    <TextBlock Text="{Binding NewValue, ElementName=root}"/>
    <TextBlock Margin="7,0,0,0" Text="{Binding OldValue, ElementName=root}">
        <TextBlock.Style>
            <Style TargetType="TextBlock">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding OldValue, ElementName=root}" Value="{x:Null}">
                        <Setter Property="Visibility" Value="Collapsed"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TextBlock.Style>
    </TextBlock>
</StackPanel>

上記のコードは、バインディングソース値(OldValueプロパティ)がnullである場合、2番目のテキストブロックが崩壊することを意味します。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top