Check Mark doesn't become visible on trigger - wpf

I have a simple CheckBox, in an Items Control - That is invisible if not checked and the mouse isn't over it the parent grid. But is visible if checked or mouse is over the grid.
The problem is, when the CheckBox is checked, the box stays visible but the check-mark disappears if the mouse isn't over the grid.
The code:
<CheckBox>
<CheckBox.Style>
<Style TargetType="{x:Type CheckBox}">
<Setter Property="Visibility" Value="Collapsed" />
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Grid}, Path=IsMouseOver}" Value="True">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Visibility" Value="Visible" />
</Trigger>
</Style.Triggers>
</Style>
</CheckBox.Style>
</CheckBox>
How do I keep the CheckMark visible in the CheckBox when the mouse moves away? As you can see in the example below, the mouse is over the center of the three, but the other two are checked as well.

Related

Change controls properties outside Tabcontrol based on selected Tab

I am working with tabcontrol in WPF.
I want a button outside the tabcontrol to change to a dropdownbutton (combobox) based on certain choices(tab page selected)
Appreciate your help.
Add both controls to your layout and toggle their visibility based on a datatrigger and the selectedindex of the tabcontrol
<ComboBox>
<ComboBox.Style>
<Style TargetType="ComboBox">
<Setter Property="Visibility" Value="Collapsed"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=yourTabControl, Path=SelectedIndex}" Value="0">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
</ComboBox>
<Button>
<Button.Style>
<Style TargetType="Button">
<Setter Property="Visibility" Value="Visible"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=yourTabControl, Path=SelectedIndex}" Value="1">
<Setter Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>

How to set ContentPresenter default content to Unset in WPF XAML

I'm trying to display status icons in status bar. The icons are defined as ViewBox static resources and displayed via ContentPresenter style with DataTriggers.
I would like no icon displayed if none of the triggers are matched, so I've tried setting the default Setter Content to x:Null or an empty string or remove the line at all, but the other icons stop displaying at all then.
Any ideas please?
My XAML code is as follows
<StatusBarItem Grid.Column="2">
<ContentPresenter>
<ContentPresenter.Style>
<Style TargetType="ContentPresenter">
<Setter Property="Content" Value="{x:Null}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=State}" Value="Ok">
<Setter Property="Content" Value="{StaticResource StatusOK}"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=State}" Value="Invalid">
<Setter Property="Content" Value="{StaticResource StatusInvalid}"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=State}" Value="Warning">
<Setter Property="Content" Value="{StaticResource StatusWarning}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentPresenter.Style>
</ContentPresenter>
</StatusBarItem>
Update
I've tried using visibility as suggested by Ed Plunkett but the icons stopped showing up at all. Here is the code.
<Style TargetType="ContentPresenter">
<Setter Property="Content" Value="{StaticResource StatusOK}"/>
<Setter Property="Visibility" Value="Collapsed"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=State}" Value="Ok">
<Setter Property="Content" Value="{StaticResource StatusOK}"/>
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=State}" Value="Invalid">
<Setter Property="Content" Value="{StaticResource StatusInvalid}"/>
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=State}" Value="Warning">
<Setter Property="Content" Value="{StaticResource StatusWarning}"/>
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
Your problem was misusing a ContentPresenter. There's no reason to use a ContentPresenter outside of a ControlTemplate. The only features it has which differ from ContentControl are things that only make sense inside a ControlTemplate. Now, as it happens, StatusBarItem is a subclass of ContentControl, so you could just style the StatusBarItem and set its Content. Either version in your question will work that way.

How to hide control when other two controls are already hidden?

I have WPF Grid with one row and three columns:
Label1 | control1 | control2
I want to set Label1 visibility to hidden when both controls are hidden:
Any easy way to do this in XAML?
you can use Multibinding and a MultiConverter on Label1.Visibility to do this job
or you use Style Trigger for your Label1 and set the initial visibility to hidden and use 2 triggers to set the visibility to visible if one of the controls are visible
EDIT
<Label x:Name=Label1>
<Label.Style>
<Style TargetType="Label">
<Setter Property="Visibility" Value="Hidden"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=ctl1, Path=Visibility}" Value="Visible">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=ctl2, Path=Visibility}" Value="Visible">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Label.Style>
</Label>

Set to visible when item in a combobox is selected

As the title says, I have a hidden border with some controls inside, and I would like to show it when a particular item in a combobox is selected.
I tried the following
<ComboBox Name="cmbRequiredRule" SelectedValuePath="Content"
SelectedValue="{Binding Path=ClientValidation.NarrativeRequiredRule}">
<ComboBoxItem>All</ComboBoxItem>
<ComboBoxItem>Matching</ComboBoxItem>
</ComboBox>
<Border Visibility="Collapsed">
<Border.Resources>
<Style TargetType="{x:Type Border}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=ClientValidation.NarrativeRequiredRule}" Value="Matching">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Resources>
....
</Border>
and this property in the view model:
public string NarrativeRequiredRule
{
get...
set...
}
but the trigger doesn't seem to be working
Try setting Visibility=Collapsed in your Style Setters, not as part of the Border Tag. I've had issues in the past where a DataTrigger would not apply when the value was specified as part of the Tag.
<Border>
<Border.Resources>
<Style TargetType="{x:Type Border}">
<Setter Property="Visibility" Value="Collapsed" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Test}" Value="Matching">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Resources>
...
</Border>
Locally assigned value takes precedence over styles. Hence you need to have
<Setter Property="Visibility" Value="Collapsed" />
in Style as #Rachel has pointed out.
Also I tried debugging the binding using a dummy converter and found that the value turned out to be System.Windows.Controls.ComboBoxItem: Matching instead of Matching.
Hence your final style is:
<Style TargetType="{x:Type Border}">
<Setter Property="Visibility" Value="Collapsed" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=ClientValidation.NarrativeRequiredRule}" Value="System.Windows.Controls.ComboBoxItem: Matching">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
could be binding issue. In your example below:
<DataTrigger Binding="{Binding Path=ClientValidation.NarrativeRequiredRule}" Value="Matching">
where is the ClientValidation located ? because if the whole View's DataContext is bound to VM, you will need to include these hierarchies. Check your Output log, it should throw some errors if binding failes

WPF DataGridCheckBoxColumn: how to hide the checkbox if the binding value is null?

I have a datagrid with a DataGridCheckBoxColumn binding to nullable bool. I'd like to hide the checkbox completely if the value is null. I tried the following trigger, but it doesn't work:
<Style TargetType="CheckBox">
<Style.Triggers>
<Trigger Property="IsChecked" Value="{x:Null}">
<Setter Property="Visibility" Value="Hidden"/>
</Trigger>
</Style.Triggers>
</Style>
Is it possible at all? Your help is much appreciated!
There are always two styles in a DataGrid, the ElementStyle and the EditingElementStyle, your style should be applied as ElementStyle, then you can still edit the checkbox but it won't be visible when not in edit mode if null. Also the three states need to be enabled.
<DataGridCheckBoxColumn Binding="{Binding MyNullableBool}" IsThreeState="True">
<DataGridCheckBoxColumn.ElementStyle>
<Style TargetType="CheckBox">
<Style.Triggers>
<Trigger Property="IsChecked" Value="{x:Null}">
<Setter Property="Visibility" Value="Hidden"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGridCheckBoxColumn.ElementStyle>
</DataGridCheckBoxColumn>

Resources