Click here to Skip to main content
14,876,363 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
VB
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


Button declaration on the xaml
HTML
<RibbonButton x:Name="btnStart" Style="{StaticResource btnTriggers}"  SmallImageSource="Images/start.ico" Label="Start"/>


and the Style
HTML
<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>



VB
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
VB
DataContext.btnStartVM = True
does the job and it triggers the NotifyPropertyChanged but it dosen`t reflect back to the UI.

Cheers :)
Posted

1 solution

cornelionut wrote:

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


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:
XML
<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>
   
Comments
cornelionut 20-May-15 6:19am
   
Your solution works great. The problem I`m facing now is accessing the DataContext and change property value from a Page that will reflect back to the mainwindow UI when NotifyPropertyChanged is triggered.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900