开发者

Manually evaluate Conditions

开发者 https://www.devze.com 2023-04-05 00:03 出处:网络
I want to set the \"Visibility\" Property of a control to a specified value when all conditions true. Like in the following example:

I want to set the "Visibility" Property of a control to a specified value when all conditions true. Like in the following example:

    <TextBlock Text="TEST">
        <e:Interaction.Behaviors>
            <bhv:VisibilityBehaviour VisibilityIfTrue="Visible" VisibilityIfFalse="Collapsed">
                <Condition Binding="{Binding BOOL1}" Value="true"/>
                <Condition Binding="{Binding BOOL2}" Value="true"/>
                <Condition Binding="{Binding BOOL3}" Value="true"/>
            </bhv:VisibilityBehaviour>
        </e:Interaction.Behaviors>
    </TextBlock>

How can i manually evaluate these Conditions? I know how to get the value but if the value changes i'm not notified.

I know i can get the value of the target of the binding using reflections. Then i can compare these both Values from the condition.

With the Attribute [ContentProperty("Conditions")] the conditions between the VisibilityBehavior tags are added to the list.

VisibilityBehavior.cs

[ContentProperty("Conditions")]
public class VisibilityBehaviour : Behavior<FrameworkElement>
{
    public static readonly DependencyProperty VisibilityProperty;
    public Visibility Visibility
    {
        get { return (Visibility)GetValue(VisibilityProperty); }
        set { SetValue(VisibilityProperty, value); }
    }

    public Visibility VisibilityIfTrue { get; set; }
    public Visibility VisibilityIfFalse { get; set; }
    public List<Condition> Conditions { get; set; }

    static VisibilityBehaviour()
    {
        VisibilityProperty = DependencyProperty.Register("Visibility", typeof(Visibility), typeof(VisibilityBehaviour), new UIPropertyMetadata(Visibility.Visible));
    }
    public VisibilityBehaviour()
    {
        Conditions = new List<Condition>();
    }

    protected override void OnAttached()
    {
        base.OnAttached();

        FrameworkElement obj = Associat开发者_高级运维edObject as FrameworkElement;
        if (obj == null)
            return;
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();

        FrameworkElement obj = AssociatedObject as FrameworkElement;
        if (obj == null)
            return;
    }
}

How can i get notified if the value of a binding changed, for an example: BOOL1 is false and then changes to true.

UPDATE: now using converters

MultiValueConverter, if all BOOLs are true, then it return Visibility.Visible, else .Collapsed

<MultiBinding Converter="{StaticResource VisibilityConverter}" ConverterParameter="true">
    <Binding Path="BOOL1"/>
    <Binding Path="BOOL2"/>
    <Binding Path="BOOL3"/>
</MultiBinding>

ValueConverter, here its not notifiying if a condition Binding value changes, but here i can add the whole conditions

<Binding Converter="{StaticResource VisibilityConverter}">
    <Binding.ConverterParameter>
        <ConditionCollection >
            <Condition Binding="{Binding BOOL1}" Value="true"/>
            <Condition Binding="{Binding BOOL2}" Value="true"/>
            <Condition Binding="{Binding BOOL3}" Value="true"/>
        </ConditionCollection>
    </Binding.ConverterParameter>
</Binding>

I need a mix between these two Converters because

  • I want to add Conditions in xaml (like in ValueConverter)
  • It needs to be updated if a value of a Binding changes

Any Ideas?


Hope I right understood your problem:

You can use IMultiValueConverter together with MultiBinding to bind your control property to more then one model's property, and inside converter evaluate a result.

Check out an example.


You dont get notified because the Binding object doesnt change. The value of the bindings Source and Target Properties do however, so you might want to expand your Condition class (if its your own) to register on TargetUpdated or SourceUpdate Events of the binding and further propagate that event.

http://msdn.microsoft.com/de-de/library/system.windows.data.binding.aspx

Also read up on that events, because there are certain conditions that need to be ensured (setting some bools on the bindings) for that events to be triggered.

Hope that helps.

Also, as another poster already mentioned, you could probably solve your problem with MultiBindings, discarding your Behaviour based concept.


You are in WPF use style triggers with a MultiDataTrigger.
http://msdn.microsoft.com/en-us/library/system.windows.multidatatrigger.aspx

<Style TargetType="TextBlock">
  <Setter Property="Visibility" Value="Collapsed" />
  <Style.Triggers>
    <MultiDataTrigger>
      <MultiDataTrigger.Conditions>
        <Condition Binding="{Binding Binding1}" Value="Condition1" />
        <Condition Binding="{Binding Binding2}" Value="Condition2" />
      </MultiDataTrigger.Conditions>
      <Setter Property="Visibility" Value="Visible" />
    </MultiDataTrigger>
  </Style.Triggers>
</Style>
0

精彩评论

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