Question

Je dois montrer anciennes et nouvelles valeurs dans mon ListView, lorsque l'enregistrement est modifié. Je veux dire chaque cellule doit montrer une nouvelle valeur et ancienne valeur. Pour l'instant je le fais:

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

pour la colonne suivante, il sera:

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

Mais j'ai 10 colonnes et ce n'est pas si intéressant de faire beaucoup de copier-coller pour les 10 colonnes.

Toute idée comment cela peut être fait plus compact et plus mieux?

La variante idéale est que je veux quelque chose comme ceci:

<GridViewColumn.CellTemplate>
    <DataTemplate>
         <MySpecialWhatever NewValueText="{Binding MyValue}" OldValueText="{Binding old.MyValue}" > 
         </MySpecialWhatever>
    </DataTemplate>
</GridViewColumn.CellTemplate>
Était-ce utile?

La solution

Vous pouvez atteindre votre objectif avec UserControl personnalisé comme celui-ci.

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>

UPDATE

Vous pouvez réduire deuxième contrôle avec DataTrigger.

<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>

Le code ci-dessus signifie que le second TextBlock réduit si la valeur de source de liaison (propriété OldValue) est nulle.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top