0

当记录发生更改时,我需要在我的 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>
4

1 回答 1

1

您可以像这样使用自定义 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 折叠第二个控件。

<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 属性)为空,则折叠第二个 TextBlock。

于 2011-01-18T13:00:01.213 回答