Why do my triggers end in empty control?

In WPF, I am trying to create a "flag" control that displays a checkmark or X based on the binding dependency property ( Flag )

 <UserControl x:Name="Root" (Other user control stuff)> <ContentControl Height="20" x:Name="flagHolder"> <ContentControl.Style> <Style TargetType="ContentControl"> <Style.Triggers> <DataTrigger Binding="{Binding ElementName=Root, Path=Flag}" Value="False"> <Setter Property="Content" Value="{StaticResource XIcon}" /> <Setter Property="Foreground" Value="Crimson"/> </DataTrigger> <DataTrigger Binding="{Binding ElementName=Root, Path=Flag}" Value="True"> <Setter Property="Content" Value="{StaticResource CheckIcon}" /> <Setter Property="Foreground" Value="ForestGreen"/> </DataTrigger> </Style.Triggers> </Style> </ContentControl.Style> </ContentControl> </UserControl> 

At startup, each icon is correct (I have several of these controls, each of which is associated with different values). However, when I switch several (one of which is turned off) turns to "on", and currently "on" is turned off), I see two things:

  • The control that was turned "on" became a green check (optional).
  • The control that has been disabled is now simply empty.

Checking the visual tree seems to indicate that everything is working (although I could easily skip something here), and the order of operation does not seem to be significant. What am I doing wrong?

Here is an example of an icon, the geometry of the path is removed from the moment of its simple noise:

 <Viewbox x:Key="CheckIcon" x:Shared="False"> <Path Style="{StaticResource IconPathStyle}"> <Path.Data> <PathGeometry Figures="Bunch of SVG" FillRule="NonZero"/> </Path.Data> </Path> </Viewbox> 
+6
source share
1 answer

I cannot reproduce your problem, but here is what I have and it works:

App.xaml

 <Application x:Class="WpfApplication1.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="MainWindow.xaml"> <Application.Resources> <Viewbox x:Key="CheckIcon" x:Shared="False"> <Canvas Height="24" Width="32"> <Path Width="7.85446" Height="8.57578" Canvas.Left="-0.0522281" Canvas.Top="-0.100391" Stretch="Fill" StrokeThickness="1.04192" StrokeMiterLimit="2.75" Stroke="#FF000000" Data="F1 M 0.468732,4.66838L 3.03345,7.95443L 7.28127,0.420569"/> </Canvas> </Viewbox> <Viewbox x:Key="XIcon" x:Shared="False"> <Canvas Height="24" Width="32"> <Path Data="M0,0 L1,1 M0,1 L1,0" Stretch="Fill" Stroke="Black" StrokeThickness="3" Width="12" Height="12" /> </Canvas> </Viewbox> </Application.Resources> </Application> 

Yesno.xaml

 <UserControl x:Class="WpfApplication1.YesNo" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="Root"> <ContentControl Height="20" Name="flagHolder"> <ContentControl.Style> <Style TargetType="ContentControl"> <Style.Triggers> <DataTrigger Binding="{Binding ElementName=Root, Path=Flag}" Value="False"> <Setter Property="Content" Value="{StaticResource XIcon}" /> <Setter Property="Foreground" Value="Crimson"/> </DataTrigger> <DataTrigger Binding="{Binding ElementName=Root, Path=Flag}" Value="True"> <Setter Property="Content" Value="{StaticResource CheckIcon}" /> <Setter Property="Foreground" Value="ForestGreen"/> </DataTrigger> </Style.Triggers> </Style> </ContentControl.Style> </ContentControl> </UserControl> 

YesNo.xaml.cs

 using System.Windows; using System.Windows.Controls; namespace WpfApplication1 { /// <summary> /// Interaction logic for UserControl1.xaml /// </summary> public partial class YesNo : UserControl { public YesNo() { InitializeComponent(); } public static readonly DependencyProperty FlagProperty = DependencyProperty.Register( "Flag", typeof(bool), typeof(YesNo), new PropertyMetadata(default(bool))); public bool Flag { get { return (bool) GetValue(FlagProperty); } set { SetValue(FlagProperty, value); } } } } 

MainWindow.xaml

 <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:wpfApplication1="clr-namespace:WpfApplication1" xmlns:system="clr-namespace:System;assembly=mscorlib" Title="" Width="400" Height="400"> <StackPanel Orientation="Vertical" Margin="50"> <wpfApplication1:YesNo Flag="{Binding Flag1}"/> <wpfApplication1:YesNo Flag="{Binding Flag2}"/> <wpfApplication1:YesNo Flag="{Binding Flag2}"/> <wpfApplication1:YesNo Flag="{Binding Flag1}"/> <Button Content="Toggle" Click="ButtonBase_OnClick"></Button> </StackPanel> </Window> 

MainWindow.xaml.cs

 public partial class MainWindow : INotifyPropertyChanged { private bool _flag1; private bool _flag2; public MainWindow() { InitializeComponent(); DataContext = this; Flag1 = true; Flag2 = false; } public bool Flag1 { get { return _flag1; } set { _flag1 = value; OnPropertyChanged(); } } public bool Flag2 { get { return _flag2; } set { _flag2 = value; OnPropertyChanged(); } } private void ButtonBase_OnClick(object sender, RoutedEventArgs e) { Flag1 = !Flag1; Flag2 = !Flag2; } } 

What does it look like:

screenshot

Video: http://www.screencast.com/t/J5IY7DR3Ry

+2
source

Source: https://habr.com/ru/post/1012388/


All Articles