开发者

XAML and local variable/auto property

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜