I'm trying to change a Button's Click property/event when a DataTrigger is triggered but I'm not sure if this is the best method to do it. In fact, it won't even compile :)
What I have looks like this:
<Style TargetType="{x:Type Button}">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=ObjectTreeView, Path=SelectedItem.Replaceable}" Value="False">
<Setter Property="Content" Value="Add" />
<Setter Property="Button.Click" Value="AddObject_Click" />
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=ObjectTreeView, Path=SelectedItem.Replaceable}" Value="True">
<Setter Property="Content" Value="Replace" />
<Setter Property="Button.Click" Value="ReplaceObject_Click" />
</DataTrigger>
</Style.Triggers>
</Style>
Compiling gives me an error message saying "Cannot find the Style Property 'Click' on the type 'System.Windows.Controls.Button'"
Any suggestions? If this not possible, what alternatives are there?
Thanks!
Edit:
I thought I found the solution which was to use an EventSetter, but EventSetters aren't supported inside Triggers. What I thought would've worked was:
<EventSetter Event="Button.Click" Handlder="AddObject_Click" />
But like I said, this is supported at all.
Wouldn't it be easier to just have one click event and in that event, an if statement based on your DataTrigger?
Click isn't a property, it's an event. Buttons have a property IsPressed that becomes true when the button is being pressed. You could try using that.
Instead of using a Click event, try using the button's Command property. You should be able to switch which command it points to based on a Trigger.
Related
XAML
<utility:InvalidNotification x:Name="InvalidNotificationControl"/>
<Button Content="Clean AppV Cache" Click="Button_Click">
<Button.Style>
<Style TargetType="Button">
<Setter Property="IsEnabled" Value="True"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=InvalidNotificationControl, Path=Visibility}" Value="Visible">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
NOTE
InvalidNotification is a custom UserControl
Now, the DataTrigger works fine initially and disable the button since the Usercontrol is visible.
The problem is when I collapse the Usercontrol based on another condition the button stays disable. I found this related answer which states that The properties changed by triggers are automatically reset to their previous value when the triggered condition is no longer satisfied. which is not my case. Why is that ?
EDIT
Thanks to #mm8 which led me to the solution. So if ever you're trying to bind a control on a UserControl's content (inner TextBlock in my case), just add a second trigger at the bottom of your Usercontrol like so,
<UserControl.Style>
<Style TargetType="UserControl">
<Setter Property="Visibility" Value="Collapsed"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=Notification, Path=Visibility}" Value="Visible">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</UserControl.Style>
where notification would be the name of my TextBlock
Your example works provided that you toggle/set the Visibility property of the InvalidNotification control itself, since it is this property that you bind to.
If you set the Visibility property of some element within the InvalidNotification control, you need to bind to this specific element.
You can't do this using an ElementName binding though because the Button and any element defined in the InvalidNotification control don't belong to the same namescope.
I have a dialog with buttons - the buttons text and functionality are dynamic and changing according to the user's needs.
In most cases the button style is the default style, in case the button is an OK button, I would like the button to use a different style.
I've tried to add a trigger that will change the button style according to a Boolean property:
(when IsOKButton=true use the "RedButtonStyle")
<Button.Template>
<ControlTemplate >
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding IsOKButton}" Value="True">
<Setter Property="Style" Value="{StaticResource RedButtonStyle}"/>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
the RedButtonStyle is a resource style that can be used in diffrent projects for a diffrent buttons type , so the soultion should be in my project and can't be in the resource style itself.
But when using this trigger I get an exception.
"set property system.windows.controls.control.template threw an exception"
Can anyone help me resolve this problem, or suggest an idea to set the style dynamicly?
Thanks
You could put the trigger in the style itself:
<Style x:Key="OKButtonStyle">
<Style.Triggers>
<DataTrigger Binding="{Binding IsOKButton}" Value="True">
//Whatever Property is different in the OKButtonStyle
</DataTrigger>
<DataTrigger Binding="{Binding IsOKButton}" Value="False">
//Set it back to default
</DataTrigger>
</Style.Triggers>
</Style>
Then just give your button the style OKButtonStyle from the start.
If want to make your OK Button mostly the same as a more general style which you want all your buttons to have you can just base it on the general one like this:
<Style x:Key="OKButtonStyle" BasedOn="{StaticResource GeneralButtonStyle}">
Im trying to make an event trigger using a button to tell the ListView each time to resize it columns automatically.
ive used different methods but non of them worked, the event is in this code defined inside the ListView and im trying through DataBinding to take the action (IsPressed) from the Button trigger the ListView.
when i try to execute the code i get the following error
The tag 'Binding' does not exist in XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation'.
im doing the DataBinding in a wrong way? will this code in this way work at all?
thanks in advance!
<ListView.Style>
<Style>
<Style.Triggers>
<Trigger Binding = "{Binding ElementName=Button1,Path=IsPressed}" Value="True">
<Setter Property="GridViewColumn.Width" Value="Auto"/>
</Trigger>
</Style.Triggers>
</Style>
</ListView.Style>
To bind a Trigger to a property outside the element scope use DataTrigger
Example:
<DataTrigger Binding="{Binding ElementName=Button1, Path=IsPressed}" Value="True">
<Setter Property="GridViewColumn.Width" Value="Auto"/>
</DataTrigger>
A regular trigger(Property Trigger) only responds to dependency properties, which you are trying as of now:
<Style.Triggers>
<Trigger .....
Use DataTrigger, it can be bound to another control
<Style.Triggers>
<DataTrigger Binding="{Binding
I want to make my gridsplitters in a custom user control have visibility collapsed
based on when the parent control has loaded some data using a load button? I thought that the way to do this would be to create a property dataloaded on the parent control and then set a trigger in the triggers of the usercontrol like below:
but I can't seem to get it to reference the property of the usercontrol (graphviewer).
Also, can property triggers reference other controls within the control like I did below? I am assuming either my syntax is wrong or what I am trying to do is not possible. So Far I have only messed with a few basic properties within trigger templates when making modifications to control templates, so I don't really know whether what I am trying to do is possible.
<UserControl.Triggers>
<Trigger Property="GraphViewer.DataLoaded" Value="true">
<Setter Property="SignalNameGridSplitter.Visibility" Value="Visible" />
</Trigger>
</UserControl.Triggers>
Try this... first add the XML Namespace of your GraphViewer control - something like this:
xmlns:YourXmlNamespace="clr-namespace:YourApplicationName.FolderNameIfApplicable"
Then add this into a Style... it has to be in a Style.Triggers collection because you can't use a DataTrigger in a UserControl.Triggers collection:
<UserControl.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding DataLoaded, RelativeSource={RelativeSource
FindAncestor, AncestorType={x:Type YourXmlNamespace:GraphViewer}}}" Value="True">
<Setter Property="SignalNameGridSplitter.Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</UserControl.Style>
I've made an application resource with a style which should be triggered if the textbox has the "IsReadOnly" property. Looks like this:
<Application.Resources>
<Style TargetType="{x:Type TextBox}" >
<Style.Triggers>
<Trigger Property="IsReadOnly" Value="True">
<Trigger.Setters>
<Setter Property="Background" Value="Black" />
</Trigger.Setters>
</Trigger>
</Style.Triggers>
</Style>
</Application.Resources>
However, the program doesn't react to this. It works when I use IsEnabled=True. However IsEnabled=False doesn't work either. So, question: Do the triggers only work if you check for "True"? And is IsReadOnly not supported at all? If so: How do I know which control properties are actually supported?
See an answer for this problem by following the provided link:
DataTrigger problem with textbox