【问题标题】:Why MultiDataTrigger Binding failed in my CustomControl?为什么我的 CustomControl 中的 MultiDataTrigger 绑定失败?
【发布时间】:2020-09-01 09:17:23
【问题描述】:

这是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>

这是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);


    }
}

这里是所有文件:

程序运行后报错:

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.'

不管我删除IsMouseOver还是MyProperty条件绑定,还是会报这个。

我的代码有什么问题?

【问题讨论】:

标签: wpf


【解决方案1】:

Condition.Property 不是DependencyProperty,也不能是Binding.Target。您必须使用Condition.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>

但由于您是在模板化对象本身上触发,您可以使用简单的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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-15
    • 2010-11-29
    • 1970-01-01
    • 2014-03-10
    • 2011-09-07
    相关资源
    最近更新 更多