1

Here is code in Generic.xaml:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApp2.Controls">


    <Style TargetType="{x:Type local:SampleControl}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:SampleControl}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                    </Border>
                    <ControlTemplate.Triggers>
                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition Property="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseOver}" Value="True" />
                                <Condition Property="{Binding RelativeSource={RelativeSource Self}, Path=MyProperty}" Value="1" />
                            </MultiDataTrigger.Conditions>
                            <MultiDataTrigger.Setters>
                                <Setter Property="Background" Value="#cde8ff"/>
                            </MultiDataTrigger.Setters>
                        </MultiDataTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

Here is code in SampleControl.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp2.Controls
{
    /// <summary>
    /// Follow steps 1a or 1b and then 2 to use this custom control in a XAML file.
    ///
    /// Step 1a) Using this custom control in a XAML file that exists in the current project.
    /// Add this XmlNamespace attribute to the root element of the markup file where it is 
    /// to be used:
    ///
    ///     xmlns:MyNamespace="clr-namespace:WpfApp2.Controls"
    ///
    ///
    /// Step 1b) Using this custom control in a XAML file that exists in a different project.
    /// Add this XmlNamespace attribute to the root element of the markup file where it is 
    /// to be used:
    ///
    ///     xmlns:MyNamespace="clr-namespace:WpfApp2.Controls;assembly=WpfApp2.Controls"
    ///
    /// You will also need to add a project reference from the project where the XAML file lives
    /// to this project and Rebuild to avoid compilation errors:
    ///
    ///     Right click on the target project in the Solution Explorer and
    ///     "Add Reference"->"Projects"->[Browse to and select this project]
    ///
    ///
    /// Step 2)
    /// Go ahead and use your control in the XAML file.
    ///
    ///     <MyNamespace:SampleControl/>
    ///
    /// </summary>
    public class SampleControl : Control
    {
        static SampleControl()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(SampleControl), new FrameworkPropertyMetadata(typeof(SampleControl)));
        }


        public int MyProperty
        {
            get { return (int)GetValue(MyPropertyProperty); }
            set { SetValue(MyPropertyProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MyPropertyProperty =
            DependencyProperty.Register("MyProperty", typeof(int), typeof(SampleControl), null);


    }
}

Here are all the files:
enter image description here

After the program ran, it reports an error:

System.Windows.Markup.XamlParseException: 'A 'Binding' cannot be set on the 'Property' property of type 'Condition'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.'

No matter I delete IsMouseOver or MyProperty condition binding, it still reports this.

What's wrong with my codes?

Melon NG
  • 2,568
  • 6
  • 27
  • 52
  • Similar: https://stackoverflow.com/q/2240421/1136211. A Condition is not a DependencyObject, You can't bind its Property property. – Clemens Sep 01 '20 at 09:23

1 Answers1

2

Condition.Property is not a DependencyProperty and cannot be a Binding.Target. You have to use the Condition.Binding property to set up a data binding:

<MultiDataTrigger>
  <MultiDataTrigger.Conditions>
    <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseOver}" Value="True" />
    <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=MyProperty}" Value="1" />
  </MultiDataTrigger.Conditions>
  <MultiDataTrigger.Setters>
    <Setter Property="Background" Value="#cde8ff"/>
  </MultiDataTrigger.Setters>
</MultiDataTrigger>

But since you are triggering on the templated object itself, you can use a simple MultiTrigger:

<MultiTrigger>
  <MultiTrigger.Conditions>
    <Condition Property="IsMouseOver" Value="True" />
    <Condition Property="MyProperty" Value="1" />
  </MultiTrigger.Conditions>
  <MultiTrigger.Setters>
    <Setter Property="Background" Value="#cde8ff"/>
  </MultiTrigger.Setters>
</MultiTrigger>
BionicCode
  • 1
  • 4
  • 28
  • 44