How can I provide multiple conditions for data trigger in WPF?

后端 未结 4 1800
臣服心动
臣服心动 2020-12-02 05:04

How can I provide multiple conditions for data trigger in WPF?

4条回答
  •  醉话见心
    2020-12-02 06:01

    To elaborate on @serine's answer and illustrate working with non-trivial multi-valued condition: I had a need to show a "dim-out" overlay on an item for the boolean condition NOT a AND (b OR NOT c).

    For background, this is a "Multiple Choice" question. If the user picks a wrong answer it becomes disabled (dimmed out and cannot be selected again). An automated agent has the ability to focus on any particular choice to give an explanation (border highlighted). When the agent focuses on an item, it should not be dimmed out even if it is disabled. All items that are not in focused are marked de-focused, and should be dimmed out.

    The logic for dimming is thus:

    NOT IsFocused AND (IsDefocused OR NOT Enabled)

    To implement this logic, I made a generic IMultiValueConverter named (awkwardly) to match my logic

    // 'P' represents a parenthesis
    //     !  a &&  ( b ||  !  c )
    class NOT_a_AND_P_b_OR_NOT_c_P : IMultiValueConverter
    {
        // redacted [...] for brevity
        public object Convert(object[] values, ...)
        {
            bool a = System.Convert.ToBoolean(values[0]);
            bool b = System.Convert.ToBoolean(values[1]);
            bool c = System.Convert.ToBoolean(values[2]);
    
            return !a && (b || !c);
        }
        ...
    }
    

    In the XAML I use this in a MultiDataTrigger in a