views:

56

answers:

2

Hi all,
I try to set a local property through XAML and a trigger.
The idea is to "reproduce" the behaviour that exists in most of settings pages with an "Apply" button at the bottom.
As soon as the user modify one the the control/settings in the page, I want to set a local variable ("FilterModified") to "true".
I tried this, but somehow, I cannot get it to compile:

<ToggleButton.Triggers>
    <Trigger Property="ToggleButton.IsChecked">
        <Setter Property="myUserControl.FilterModified" Value="True"/>
    </Trigger>
</ToggleButton.Triggers>

In the code, FilterModified is declare like this:

public partial class myUserControl: UserControl
{
    public static Boolean FilterModified { get; set; }

Could someone please help me?
Thx in advance.
Fred

+1  A: 

It would help if you posted the compile error you're getting, but I suspect you need to make FilterModified a dependency property instead of a regular one. The easiest way is to type propdp in Visual Studio, press Tab and set the highlighted fields to the correct values:

public bool FilterModified
{
    get { return (bool)GetValue(FilterModifiedProperty); }
    set { SetValue(FilterModifiedProperty, value); }
}

// Using a DependencyProperty as the backing store for FilterModified.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty FilterModifiedProperty =
        DependencyProperty.Register("FilterModified", typeof(bool), typeof(ownerclass), new UIPropertyMetadata(false));

With this the XAML you posted should work, I think.

Edit: however, of course, I'd suggest looking into MVVM (Model-View-ViewModel) for doing things like this, and leaving only the bindings do the heavy lifting; that would mean moving the FilterModified into the ViewModel and handling it only from there, instead of from the code-behind which is tied to that exact control.

Alex Paven
Is it so "complicated"? What I need is really trivial. I could implement the 'Check' and 'Uncheck' event, but I'm sure, it should be possible to do it with XAML.I changed the 'Setter' line to '<Setter TargetName="local:MainUserControl.FilterModified" Value="True"/>'.Now it compiles, but the Designer complains with "Error 5 Value 'True' cannot be assigned to property 'Value'. Object reference not set to an instance of an object."
Fred
I've implemented your answer. Now it compiles, but the Designer says: Error 5 Triggers collection members must be of type EventTrigger.No idea what's wrong!
Fred
Yes; actually there's a restriction on what triggers you can place inline in a control: only event triggers (EventTrigger); all other triggers can only be placed in styles. So the alternative would be to define a default style for your control (which is always recommended anyway) and place the trigger in that style. Don't have the time right now to show you a sample, maybe when I get home.
Alex Paven
So, I've implemented it in a style. Everything is compiling, the Designer works again and I get the application to start. BUT, the property FilterModified stays on 'False' even I check/uncheck the toggle button :-(
Fred
A: 

Thx Alex Paven for the answer.
Is it so "complicated"? What I need is really trivial.
I could implement the 'Check' and 'Uncheck' event, but I'm sure, it should be possible to do it with XAML.
I changed the 'Setter' line to

<Setter TargetName="local:myUserControl.FilterModified" Value="True"/>

Now it compiles, but the Designer complains with:

"Error    5    Value 'True' cannot be assigned to property 'Value'. Object reference not set to an instance of an object."

If I put this:

<Setter Property="{x:Static local:myUserControl.FilterModified}" Value="True"/>

It compiles too, but the Designer says:

Error    4    Property 'Property' does not support values of type 'Boolean'.

Very strange, isn't it?

Fred

Fred
You say what you need is "really trivial," but you can't do it yourself and nobody else has an answer. That suggests it's not as trivial as you think!
Dan Puzey
Yes that's right. I mean, the idea is trivial!And for sure, the solution is trivial too. When I see, what can be done with a couple of xaml lines... this idea must have a trivial implementation, but which one? that's the question!
Fred