I have the same options in different WPF dialog. One option that depends on another. If I click on the first, I also want to check the second. Both options correspond to the private bool variable in different classes. What is the best solution for this problem?
You could do that in WPF/XAML with Style.Triggers. For example the following code is an example on how to bind a textblock control's IsEnabled status based on the status of a checkbox (checked/unchecked)
<TextBlock Text="mytextblockText: ">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding IsChecked,ElementName=myCheckbox}" Value="False">
<Setter Property="IsEnabled" Value="True"/>
</DataTrigger>
<DataTrigger Binding="{Binding IsChecked, ElementName=myCheckbox}" Value="True">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
For a checkbox being checked when another checkbox is checked, it looks like this (I guess)
<CheckBox>
<CheckBox.Style>
<Style TargetType="CheckBox">
<Style.Triggers>
<DataTrigger Binding="{Binding IsChecked,ElementName=myCheckbox}" Value="False">
<Setter Property="IsChecked" Value="True"/>
</DataTrigger>
<DataTrigger Binding="{Binding IsChecked, ElementName=myCheckbox}" Value="True">
<Setter Property="IsChecked" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</CheckBox.Style>
</CheckBox>
*edit
can the properties in the classes be changed by something different than by those checkboxes? If yes, you could fire an event when the property is changed or directly manipulate the checkstate of the according box. Also, are the boxes dependent on each other? Or is just one dependent on the other (A <=> B or A => B)?
For manipulating the properties when one of the boxe's state is changed you could use the Checked/Unchecked events. Or use DependencyProperty as described in Pat's answere over here
Related
I would like to make a template style, which I can use as a static resource, like this:
<TextBox
Style="{StaticResource CollapsingTextBox}"
Text="{Binding SomeNullableValue}" />
Now as the name suggests, I would like the text box to collapse, when the binding value is null. Normally I would do this with a data trigger, like this:
<DataTrigger Binding="{Binding SomeNullableValue}" Value="{x:null}">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
But if I move the data trigger to a template style, which can be reused, then I it's no good to bind directly to the binding property (i.e. SomeNullableValue). Instead I need to bind the the binding of the template user (if that makes sense).
How do I achieve this kind of binding?
try a Trigger with Text property:
<Trigger Property="Text" Value="">
<Setter Property="Visibility" Value="Collapsed" />
</Trigger>
<Trigger Property="Text" Value="{x:null}">
<Setter Property="Visibility" Value="Collapsed" />
</Trigger>
Is the any way to make Checked and Unchecked only fire when button clicked by user?
IsChecked is bound to Playing property. This value may change from background (not by user). So it causes Checked and Unchecked events to fire again and conflict with other things.
When ever I try to write code to fix this it becomes real mess. Thanks for your help.
<ToggleButton x:Name="TogglePlay" Width="90"
Style="{DynamicResource TogglePlay}"
IsChecked="{Binding SessionData.Playing}"
Checked="TogglePlay_Checked" Unchecked="TogglePlay_OnUnchecked"/>
<StackPanel.Resources>
<Style x:Key="TogglePlay" TargetType="ToggleButton">
<Style.Triggers>
<DataTrigger Binding="{Binding IsChecked , ElementName=TogglePlay}" Value="False">
<Setter Property="Content" Value="Play" />
</DataTrigger>
<DataTrigger Binding="{Binding IsChecked , ElementName=TogglePlay}" Value="True">
<Setter Property="Content" Value="Pause" />
</DataTrigger>
</Style.Triggers>
</Style>
</StackPanel.Resources>
You can try to use binding mode "OneWayToSource"
XAML :
<RadioButton x:Name="RadioButtonA">
<RadioButton.Style>
<Style TargetType="RadioButton">
<Setter Property="IsChecked" Value="{Binding Path=RadioAValue}" />
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=CheckBoxA, Path=IsChecked}" Value="True">
<Setter Property="IsChecked" Value="True"/>
</DataTrigger>
</Style.Triggers>
</Style>
</RadioButton.Style>
</RadioButton>
C# Code behind
public bool RadioAValue
{
get
{
...
}
set
{
...
}
}
set section of RadioAvalue will not be called when CheckBoxA is checked.
Any reason why?
Problem in your code
You are setting the binding in Style Setter
<Setter Property="IsChecked" Value="{Binding Path=RadioAValue}" />
and on clicking the checkbox you have overwriting the Binding with the value False. Now there is no more Bindings over the element and it wont call the set anymore.
Also, if you try to bind it over the RadioButton itself instead of setters that will take more precedence and your Triggers won't work there.
Solution
I would recommend you to have Command binding over the Checkbox and update the value RadioAValue from your ViewModel
If you want conditional setter use MultiBinding, right now you block binding with already setted value
<Setter Property="IsChecked" Value="{Binding Path=RadioAValue}" />
Must be something like this (didn't test thought):
<RadioButton x:Name="YouRadioButton">
<RadioButton.Style>
<Style TargetType="RadioButton">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding ElementName=CheckBoxA, Path=IsChecked}" Value="True"/>
<Condition Binding="{Binding RadioAValue}" Value="True"/>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="IsChecked" Value="True"/>
</MultiDataTrigger.Setters>
</MultiDataTrigger>
<DataTrigger Binding="{Binding ElementName=CheckBoxA, Path=IsChecked}" Value="False">
<Setter Property="IsChecked" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</RadioButton.Style>
</RadioButton>
<CheckBox x:Name="CheckBoxA"/>
i have this XAML that looks to see if a row can be
enabled or not inside a DataGrid
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding State, Converter={StaticResource converter}}" Value="true">
<Setter Property="IsEnabled" Value="true"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
The converter is working and returns false or true, however when it sends back false, the row is still enabled, have I done anything wrong here?
The default value for IsEnabled is true. So it will always be true unless you specify in a trigger for it to be false.
You should do the following:
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding State, Converter={StaticResource converter}}" Value="False">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
Therefore, it'll stay true by default and switch to false only when this trigger is activated.
i have two check boxes and a text box. the text box's IsEnabled property should be binded to both of those check box's IsChecked Properties. if one of check box or both of them are enabled then text box should be enabled.
the first solution that came to my mind was multibinding. But it needs another class to do. Is there any easier way to do this?
You can do that with two DataTriggers on TextBox Style where you can enable TextBox in case any checkBox is checked.
<CheckBox x:Name="checkBox1"/>
<CheckBox x:Name="checkBox2"/>
<TextBox>
<TextBox.Style>
<Style TargetType="TextBox">
<Setter Property="IsEnabled" Value="False"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsChecked, ElementName=checkBox1}"
Value="True">
<Setter Property="IsEnabled" Value="True"/>
</DataTrigger>
<DataTrigger Binding="{Binding IsChecked, ElementName=checkBox2}"
Value="True">
<Setter Property="IsEnabled" Value="True"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
I think not - I would really like "and" or "or" expressions in xmal, but they are not provided.
I stumpled upon this library, which claims to add them:
http://wpfconverters.codeplex.com/
But I never used them extensively.