Created
May 29, 2022 08:31
-
-
Save emoacht/2bb1930db68cde714e853873df7e6e86 to your computer and use it in GitHub Desktop.
Test bindings by nested properties through Border.Child and Border.Parent.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ValueConversion(typeof(bool), typeof(Brush))] | |
public class BooleanToBrushConverter : IValueConverter | |
{ | |
public Brush TrueBrush { get; set; } = Brushes.Gray; | |
public Brush FalseBrush { get; set; } = Brushes.Gray; | |
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) | |
{ | |
return value switch | |
{ | |
true => TrueBrush, | |
false => FalseBrush, | |
_ => DependencyProperty.UnsetValue | |
}; | |
} | |
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) | |
{ | |
throw new NotSupportedException(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Globalization; | |
using System.Windows; | |
using System.Windows.Data; | |
[ValueConversion(typeof(bool), typeof(Thickness))] | |
public class BooleanToThicknessConverter : IValueConverter | |
{ | |
public Thickness TrueThickness { get; set; } = new Thickness(0); | |
public Thickness FalseThickness { get; set; } = new Thickness(0); | |
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) | |
{ | |
return value switch | |
{ | |
true => TrueThickness, | |
false => FalseThickness, | |
_ => DependencyProperty.UnsetValue | |
}; | |
throw new NotImplementedException(); | |
} | |
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) | |
{ | |
throw new NotSupportedException(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Window x:Class="WpfApp.MainWindow" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:local="clr-namespace:WpfApp" | |
Title="MainWindow" | |
Width="400" Height="300"> | |
<Window.Resources> | |
<local:BooleanToThicknessConverter x:Key="BooleanToThicknessConverterKey" TrueThickness="8" FalseThickness="4"/> | |
<local:BooleanToBrushConverter x:Key="BooleanToBrushConverterKey" TrueBrush="Orchid" FalseBrush="Gray"/> | |
</Window.Resources> | |
<StackPanel> | |
<Border BorderThickness="{Binding ElementName=Toggle, Path=IsChecked, Converter={StaticResource BooleanToThicknessConverterKey}}" | |
BorderBrush="{Binding RelativeSource={RelativeSource Self}, Path=Child.IsChecked, Converter={StaticResource BooleanToBrushConverterKey}}"> | |
<Border.Style> | |
<Style TargetType="{x:Type Border}"> | |
<Setter Property="Background" Value="DarkGray"/> | |
<Style.Triggers> | |
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Child.IsChecked}" Value="True"> | |
<Setter Property="Background" Value="Red"/> | |
</DataTrigger> | |
</Style.Triggers> | |
</Style> | |
</Border.Style> | |
<ToggleButton x:Name="Toggle" | |
Margin="8"> | |
<Border BorderThickness="{Binding ElementName=Toggle, Path=IsChecked, Converter={StaticResource BooleanToThicknessConverterKey}}" | |
BorderBrush="{Binding RelativeSource={RelativeSource Self}, Path=Parent.IsChecked, Converter={StaticResource BooleanToBrushConverterKey}}"> | |
<Border.Style> | |
<Style TargetType="{x:Type Border}"> | |
<Setter Property="Background" Value="DarkGray"/> | |
<Style.Triggers> | |
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Parent.IsChecked}" Value="True"> | |
<Setter Property="Background" Value="Orange"/> | |
</DataTrigger> | |
</Style.Triggers> | |
</Style> | |
</Border.Style> | |
<TextBlock Text="Toggle"/> | |
</Border> | |
</ToggleButton> | |
</Border> | |
</StackPanel> | |
</Window> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The binding through Border.Child does not work while the binding through Border.Parent does.