Los valores dobles en células Listview
-
12-10-2019 - |
Pregunta
necesito para mostrar los valores antiguos y nuevos en mi ListView, cuando se cambia el registro. Me refiero a cada célula debe demostrar que el nuevo valor y el valor antiguo. Por ahora lo estoy haciendo así que:
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding MyValue}"/>
<TextBlock Margin="7,0,0,0" Text="{Binding old.MyValue}"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
En la columna siguiente será:
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding MySecondValue}"/>
<TextBlock Margin="7,0,0,0" Text="{Binding old.MySecondValue}"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
Pero tengo 10 columnas y esto no es tan interesante para hacer un montón de copiar y pegar para todas las columnas 10.
Alguna idea de cómo esto se puede hacer más compacto y más mejor?
La variante ideales que quiero es algo como esto:
<GridViewColumn.CellTemplate>
<DataTemplate>
<MySpecialWhatever NewValueText="{Binding MyValue}" OldValueText="{Binding old.MyValue}" >
</MySpecialWhatever>
</DataTemplate>
</GridViewColumn.CellTemplate>
Solución
Se puede lograr su objetivo con el control de usuario personalizado como este.
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>
Actualizar
Se puede colapsar segundo control con 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>
Los medios de código por encima de la segunda TextBlock está contraído si el valor de origen de enlace (OldValue propiedad) es nulo.