不能使用IsSelected作为Trigger,但它有一个内部get访问器

时间:2016-07-13 10:25:22

标签: c# wpf wpf-controls menuitem

当我尝试在MenuItem上创建样式触发器并使用IsSelected属性作为触发器时,我收到错误:

  

'IsSelected'不能设置为Trigger的Property属性的值,因为它没有public或internal get访问器。 [...]

我的样式定义如下(错误在第3行,在Property="IsSelected"上):

<Style TargetType="{x:Type MenuItem}">
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Foreground" Value="Red"></Setter>
        </Trigger>
    </Style.Triggers>
</Style>

我检查了参考源以获取menuItem的实现。

确实是内部的:

    ///     True if this MenuItem is the current MenuItem of its parent.
    ///     Focus drives Selection, but not vice versa.  This will enable 
    ///     focusless menus.
    /// </summary>
    internal bool IsSelected
    { 
        get { return (bool) GetValue(IsSelectedProperty); }
        set { SetValue(IsSelectedProperty, BooleanBoxes.Box(value)); } 
    } 

    /// <summary> 
    ///     DependencyProperty for IsSelected property.
    /// </summary>
    internal static readonly DependencyProperty IsSelectedProperty =
            Selector.IsSelectedProperty.AddOwner( 
                    typeof(MenuItem),
                    new FrameworkPropertyMetadata( 
                            BooleanBoxes.FalseBox, 
                            FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
                            new PropertyChangedCallback(OnIsSelectedChanged))); 

为什么它不适用于触发器呢?

0 个答案:

没有答案