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

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>

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 use a data trigger in a text box that uses the selectedItem of a dataGrid?

I want to use a data trigger in a text box to set the isEnabled property according to the value of a property of the selected item in a data grid.
I am trying this:
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<DataTrigger Binding="{Binding MyDataGridName, ElementName=SelectedItem.MyProperty1.MyProperty2}" Value="1">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
But the textBox is always enabled. I check that really when I select the item, it has property1 and property2 has 1 as value. So I guess that the problem is that I don't set the trigger correctly.
try this code, you need to specify the Datagrid as elementName not the selectedItem:
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=MyDataGridName, Path=SelectedItem.MyProperty1.MyProperty2}" Value="1">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>

Check Mark doesn't become visible on trigger

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.

Best way to have a TextBox show a message based on a bool in the ModelView

I have a TextBox that needs to have its border change color and then display a message below the text box. This message should be displayed/hidden based on a bool value in the model. What is the best way to achieve this?
There are a ton of different ways of doing this. If you're only going to do this once, the simplest way is to add the TextBlock to the layout and use a style to hide it, e.g.:
<StackPanel>
<TextBox Text="{Binding Text, Mode=TwoWay}">
<TextBox.Style>
<Style TargetType="TextBox">
<Setter Property="BorderBrush" Value="Lightgray"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsValid}" Value="False">
<Setter Property="BorderBrush" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
<TextBlock Text="This is the message displayed if IsValid is false.">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Visibility" Value="Collapsed"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsValid}" Value="False">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</StackPanel>
If this is something you want to be able to repeat, you'll want to make this into a template or a user control.
Also, note that changing the visibility from collapsed to visible will change the overall layout, which could have all kinds of undesirable effects. Depending on your design, you might make the visibility default to hidden.
You can use a DataTrigger to set the text, visibility, and appearance of the textbox based on the value in the ViewModel. This seems like the simplest solution.
<TextBox>
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=TheBoolean}" Value="True">
<Setter Property="Visibility" Value="Visible" />
<Setter Property="Background" Value="Red" />
...
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
Another option is to create an IValueConverter to convert the bool to get the text, visibility, and color.

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