WPF按钮属性绑定到代码

问题描述:

大家好,



我正在尝试将功能区按钮属性IsEnabled绑定到代码属性,以便我可以根据特定情况将其提供给用户。



我不知道自己做错了什么我无法弄明白



目前我有以下代码。



属性和事件的ViewModel类

Hello all,

I am trying to bind ribbon buttons property IsEnabled to code property so I can make it available or not to the user depending on a certain situation.

I don`t know what I am doing wrong and I can`t figure it out

Currently I have the following code.

The class ViewModel for the property and the event

Public Class ViewModel

    Implements INotifyPropertyChanged



    Private _btnStart As Boolean
    


    Public Property btnStartVM() As Boolean
        Get
            Return _btnStart
        End Get
        Set(ByVal value As Boolean)
            _btnStart = value
            NotifyPropertyChanged("btnStart")
        End Set
    End Property



    Public Event PropertyChanged As PropertyChangedEventHandler _
    Implements INotifyPropertyChanged.PropertyChanged

    Public Sub NotifyPropertyChanged(ByVal propertyName As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    End Sub
End Class





xaml上的按钮声明



Button declaration on the xaml

<RibbonButton x:Name="btnStart" Style="{StaticResource btnTriggers}"  SmallImageSource="Images/start.ico" Label="Start"/>





和风格



and the Style

<Style x:Key="btnTriggers" TargetType="{x:Type RibbonButton}">
        <Style.Triggers>
               <DataTrigger Binding="{Binding Path=btnStartVM , ElementName=btnStart}" Value="True">
                   <Setter Property="IsEnabled" Value="True"/>
               </DataTrigger>
               <DataTrigger Binding="{Binding Path=btnStartVM, ElementName=btnStart}" Value="False">
                   <Setter Property="IsEnabled" Value="False"/>
               </DataTrigger>
           </Style.Triggers>
       </Style>










Public Sub Window_Loaded(ByVal sender As System.Object, ByVal e As RoutedEventArgs) Handles MyBase.Loaded
        InitializeComponent()
        DataContext = New ViewModel()

        DataContext.btnStartVM = True
End Sub







The

DataContext.btnStartVM = True

完成工作并触发NotifyPropertyChanged,但它反映回用户界面。



欢呼:)

does the job and it triggers the NotifyPropertyChanged but it dosen`t reflect back to the UI.

Cheers :)

cornelionut写道:
cornelionut wrote:




Binding="{Binding Path=btnStartVM , ElementName=btnStart}"






表示触发器直接在名为 btnStart 的元素上绑定到名为 btnStartVM 的属性。由于 btnStart RibbonButton ,因此它没有名为的属性btnStartVM



尝试从绑定中删除 ElementName


That tells the trigger to bind to a property called btnStartVM directly on the element called btnStart. Since btnStart is a RibbonButton, it doesn't have a property called btnStartVM.

Try removing the ElementName from your bindings:

<Style x:Key="btnTriggers" TargetType="{x:Type RibbonButton}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=btnStartVM}" Value="True">
            <Setter Property="IsEnabled" Value="True"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=btnStartVM}" Value="False">
            <Setter Property="IsEnabled" Value="False"/>
        </DataTrigger>
    </Style.Triggers>
</Style>