Bueno, esto no utiliza DataTriggers, pero funciona bastante bien, y creo que hace lo que usted está buscando:
MainWindow.xaml
<Window x:Class="Wpf1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="200" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid x:Name="myGrid" Grid.Row="0" Grid.ColumnSpan="2" Background="AliceBlue">
<Grid.Style>
<Style TargetType="Grid">
<Setter Property="Margin" Value="{Binding Margin}" />
</Style>
</Grid.Style>
</Grid>
<Button Content="Left Toggle" Name="LeftButton" Grid.Row="1" Grid.Column="0" />
<Button Content="Right Toggle" Name="RightButton" Grid.Row="1" Grid.Column="1" />
</Grid>
</Window>
MainWindow.xaml.cs
using System.Windows;
namespace Wpf1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
MainWindowVM mainWindowVM = new MainWindowVM(this);
this.LeftButton.Click += mainWindowVM.LeftButton_Click;
this.RightButton.Click += mainWindowVM.RightButton_Click;
DataContext = mainWindowVM;
}
}
}
Y la vista del modelo: MainWindowVM.cs
using System.Windows;
using System.ComponentModel;
namespace Wpf1
{
class MainWindowVM : INotifyPropertyChanged
{
public MainWindow MainWindow { get; set; }
private Thickness _margin;
public Thickness Margin
{
get { return _margin; }
set {
if (_margin != value)
{
_margin = value;
OnPropertyChanged("Margin");
}
}
}
private bool _rightPanelPinned;
public bool RightPanelPinned
{
get { return _rightPanelPinned; }
set
{
if (_rightPanelPinned != value)
{
_rightPanelPinned = value;
if (_rightPanelPinned == true)
{
Thickness thickness = Margin;
thickness.Right = 30.0;
Margin = thickness;
}
else
{
Thickness thickness = Margin;
thickness.Right = 0.0;
Margin = thickness;
}
}
}
}
private bool _leftPanelPinned;
public bool LeftPanelPinned
{
get { return _leftPanelPinned; }
set
{
if (_leftPanelPinned != value)
{
_leftPanelPinned = value;
if (_leftPanelPinned == true)
{
Thickness thickness = Margin;
thickness.Left = 30.0;
Margin = thickness;
}
else
{
Thickness thickness = Margin;
thickness.Left = 0.0;
Margin = thickness;
}
}
}
}
public MainWindowVM(MainWindow mainWindow)
{
MainWindow = mainWindow;
LeftPanelPinned = false;
RightPanelPinned = false;
}
public void LeftButton_Click(object sender, RoutedEventArgs e)
{
MainWindow.BeginInit();
LeftPanelPinned = (!LeftPanelPinned);
MainWindow.EndInit();
MainWindow.UpdateLayout();
}
public void RightButton_Click(object sender, RoutedEventArgs e)
{
MainWindow.BeginInit();
RightPanelPinned = (!RightPanelPinned);
MainWindow.EndInit();
MainWindow.UpdateLayout();
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Me pareció más fácil vincular el Margen de la cuadrícula a una propiedad en el modelo de vista. ¡Espero que esto ayude!
Cheers,
Andrew
no se t él cancelar el en caso de que tenga ambos? –
David
He editado el XAML para incluir un MultiDataTrigger que los establecerá a ambos. –
Gracias, esto lo clavó :) –