Answered by:
Binding Enabled property of Control

Question
-
Is there any way to bind the Enabled property of a WPF control to an object property similar to the way you can bind the content of a control.Friday, May 15, 2009 4:44 PM
Answers
-
Have got this to work now using
IsEnabled="{Binding Path=CanEditProperty}"
CanEditProperty is based on value of another property. Need to call OnPropertyChanged within property on which CanEditProperty is based.Friday, May 15, 2009 4:59 PM -
You can bind the IsEnable to bool type DependencyProperty.
For example:
XAML
<Window x:Class="IsEnabled_trigger_20081215.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:IsEnabled_trigger_20081215" Title="Window1" Name="win1" Height="298" Width="466" > <Grid Height="209" Width="405"> <ListView Name="lv" Margin="12,58,12,12"> <ListView.View> <GridView> <GridViewColumn Header="Delete"> <GridViewColumn.CellTemplate> <DataTemplate> <Button Width="85" Content="Delete" x:Name="btnDelete"> <Button.IsEnabled> <Binding ElementName="win1" Path="IsModifyAllowed"/> </Button.IsEnabled> </Button> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> <GridViewColumn DisplayMemberBinding= "{Binding Path=FirstName}" Header="First Name" Width="100"/> <GridViewColumn DisplayMemberBinding= "{Binding Path=LastName}" Header="Last Name" Width="100"/> <GridViewColumn DisplayMemberBinding= "{Binding Path=EmployeeNumber}" Header="Employee No." Width="100"/> </GridView> </ListView.View> </ListView> <Button Height="23" HorizontalAlignment="Left" Margin="12,20,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click">Enable</Button> <Button Height="23" HorizontalAlignment="Left" Margin="93,20,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click">Disable</Button> </Grid> </Window>
C#
using System; using System.Windows; using System.Collections.ObjectModel; using System.ComponentModel; namespace IsEnabled_trigger_20081215 { public partial class Window1 : Window { public DependencyProperty IsModifyAllowedProperty = DependencyProperty.Register("IsModifyAllowed", typeof(Boolean), typeof(Window1), new FrameworkPropertyMetadata()); public event PropertyChangedEventHandler PropertyChanged; public bool IsModifyAllowed { get { return (bool)GetValue( IsModifyAllowedProperty) ; } set { SetValue(IsModifyAllowedProperty, value) ; NotifyPropertyChanged("IsModifyAllowed"); } } private void NotifyPropertyChanged(String info) { if ( PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } public Employees GetEmployees() { Employees a = new Employees(); a.Add(new EmployeeInfo("Jesper", "Aaberg", "12345" )); a.Add(new EmployeeInfo("Dominik", "Paiha", "98765" )); a.Add(new EmployeeInfo("Yale", "Li", "23875" )); a.Add(new EmployeeInfo("Muru", "Subramani", "49392" )); return a; } public Window1() { InitializeComponent(); Employees a = GetEmployees(); lv.ItemsSource = a; } private void button1_Click(object sender, RoutedEventArgs e) { IsModifyAllowed = true; } private void button2_Click(object sender, RoutedEventArgs e) { IsModifyAllowed = false; } } public class EmployeeInfo { private string _firstName; private string _lastName; private string _employeeNumber; public string FirstName { get { return _firstName; } set { _firstName = value; } } public string LastName { get { return _lastName; } set { _lastName = value; } } public string EmployeeNumber { get { return _employeeNumber; } set { _employeeNumber = value; } } public EmployeeInfo(string firstname, string lastname, string empnumber) { _firstName = firstname; _lastName = lastname; _employeeNumber = empnumber; } } public class Employees : ObservableCollection<EmployeeInfo> { public Employees() { } } }
Tuesday, May 19, 2009 8:45 AM
All replies
-
Have got this to work now using
IsEnabled="{Binding Path=CanEditProperty}"
CanEditProperty is based on value of another property. Need to call OnPropertyChanged within property on which CanEditProperty is based.Friday, May 15, 2009 4:59 PM -
You can bind the IsEnable to bool type DependencyProperty.
For example:
XAML
<Window x:Class="IsEnabled_trigger_20081215.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:IsEnabled_trigger_20081215" Title="Window1" Name="win1" Height="298" Width="466" > <Grid Height="209" Width="405"> <ListView Name="lv" Margin="12,58,12,12"> <ListView.View> <GridView> <GridViewColumn Header="Delete"> <GridViewColumn.CellTemplate> <DataTemplate> <Button Width="85" Content="Delete" x:Name="btnDelete"> <Button.IsEnabled> <Binding ElementName="win1" Path="IsModifyAllowed"/> </Button.IsEnabled> </Button> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> <GridViewColumn DisplayMemberBinding= "{Binding Path=FirstName}" Header="First Name" Width="100"/> <GridViewColumn DisplayMemberBinding= "{Binding Path=LastName}" Header="Last Name" Width="100"/> <GridViewColumn DisplayMemberBinding= "{Binding Path=EmployeeNumber}" Header="Employee No." Width="100"/> </GridView> </ListView.View> </ListView> <Button Height="23" HorizontalAlignment="Left" Margin="12,20,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click">Enable</Button> <Button Height="23" HorizontalAlignment="Left" Margin="93,20,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click">Disable</Button> </Grid> </Window>
C#
using System; using System.Windows; using System.Collections.ObjectModel; using System.ComponentModel; namespace IsEnabled_trigger_20081215 { public partial class Window1 : Window { public DependencyProperty IsModifyAllowedProperty = DependencyProperty.Register("IsModifyAllowed", typeof(Boolean), typeof(Window1), new FrameworkPropertyMetadata()); public event PropertyChangedEventHandler PropertyChanged; public bool IsModifyAllowed { get { return (bool)GetValue( IsModifyAllowedProperty) ; } set { SetValue(IsModifyAllowedProperty, value) ; NotifyPropertyChanged("IsModifyAllowed"); } } private void NotifyPropertyChanged(String info) { if ( PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } public Employees GetEmployees() { Employees a = new Employees(); a.Add(new EmployeeInfo("Jesper", "Aaberg", "12345" )); a.Add(new EmployeeInfo("Dominik", "Paiha", "98765" )); a.Add(new EmployeeInfo("Yale", "Li", "23875" )); a.Add(new EmployeeInfo("Muru", "Subramani", "49392" )); return a; } public Window1() { InitializeComponent(); Employees a = GetEmployees(); lv.ItemsSource = a; } private void button1_Click(object sender, RoutedEventArgs e) { IsModifyAllowed = true; } private void button2_Click(object sender, RoutedEventArgs e) { IsModifyAllowed = false; } } public class EmployeeInfo { private string _firstName; private string _lastName; private string _employeeNumber; public string FirstName { get { return _firstName; } set { _firstName = value; } } public string LastName { get { return _lastName; } set { _lastName = value; } } public string EmployeeNumber { get { return _employeeNumber; } set { _employeeNumber = value; } } public EmployeeInfo(string firstname, string lastname, string empnumber) { _firstName = firstname; _lastName = lastname; _employeeNumber = empnumber; } } public class Employees : ObservableCollection<EmployeeInfo> { public Employees() { } } }
Tuesday, May 19, 2009 8:45 AM