How to disable combobox when command canExecute returns false.
-
Thursday, April 02, 2009 7:11 PMI have a combobox that contains a button in its template.
The button has a command attached. When canExecute returns false on that command, then the button IsEnabled is false.
I would like the IsEnabled on the whole combobox to become false in this case. I tried to do it in a trigger, but it complains because
"'IsEnabled' property cannot be set from a property trigger and appear in the trigger's condition at the same time."
How should I do this?
All Replies
-
Friday, April 03, 2009 12:08 AMCan you post the template?
WNC -
Monday, April 06, 2009 9:00 AMFor more detailed information, can you paste your full version code here?
You can use the brush like icon on the top-right of the editor.
Then I can compile your program and try to add the function or feature you have asked. -
Monday, April 06, 2009 6:47 PM
Here's what's in the control template:
<Border x:Name="Border" BorderThickness="1" > <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <Button Style="{StaticResource DropDownMainButton}" Name="ContentSite" Command="{TemplateBinding Command}" Content="{TemplateBinding Content}" BorderThickness="0" VerticalAlignment="Stretch" VerticalContentAlignment="Stretch" HorizontalAlignment="Stretch" HorizontalContentAlignment="Left" Margin="0" Padding="0" /> <ToggleButton Grid.Column="1" Name="ToggleButton" HorizontalAlignment="Right" Template="{StaticResource DropDownToggleButton}" Margin="0" Focusable="false" IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press"> </ToggleButton> <Popup Name="Popup" Placement="Bottom" IsOpen="{TemplateBinding IsDropDownOpen}" AllowsTransparency="True" Focusable="False" PopupAnimation="Slide"> <Grid Name="DropDown" SnapsToDevicePixels="True" MinWidth="{TemplateBinding ActualWidth}" MaxHeight="{TemplateBinding MaxDropDownHeight}"> <Border x:Name="DropDownBorder" Background="{StaticResource Control}" TextBlock.Foreground="{StaticResource ForeColor}" BorderThickness="1" BorderBrush="{StaticResource SolidBorderBrush}"/> <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True"> <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" /> </ScrollViewer> </Grid> </Popup> </Grid> </Border> -
Wednesday, April 08, 2009 6:12 AM
You could write some code in the loaded event of the Button. Set the ComoBox's IsEnable the same as Button's IsEnable.
For example:
XAML
<Window x:Class="_demo.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:_demo" Title="Window1" Height="300" Width="300"> <Window.CommandBindings> <CommandBinding Command="{x:Static local:Window1.ColorCmd}" Executed="ColorCmdExecuted" CanExecute="ColorCmdCanExecute"/> </Window.CommandBindings> <Grid> <ComboBox x:Name="myComboBox" Margin="10,0" Height="42" VerticalAlignment="Top"> <ComboBox.ItemTemplate> <DataTemplate> <StackPanel> <Button Loaded="Button_Loaded" Command="{x:Static local:Window1.ColorCmd}"> <Binding Path="Name"></Binding> </Button> </StackPanel> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox> </Grid> </Window>
C#
using System.Collections.Generic; using System.Windows; using System.Windows.Controls; using System.Windows.Input; namespace _demo { /// <summary> /// Interaction logic for Window1.xaml /// </summary> public partial class Window1 : Window { List<MyClass> listData = new List<MyClass>(); public static RoutedCommand ColorCmd = new RoutedCommand(); public Window1() { InitializeComponent(); listData.Add(new MyClass("a", "Description of a")); myComboBox.ItemsSource = listData; } // ExecutedRoutedEventHandler for the custom color command. private void ColorCmdExecuted(object sender, ExecutedRoutedEventArgs e) { } // CanExecuteRoutedEventHandler for the custom color command. private void ColorCmdCanExecute(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = false; } private void Button_Loaded(object sender, RoutedEventArgs e) { Button btn = sender as Button; if (btn != null) { myComboBox.IsEnabled = btn.IsEnabled; } } } public class MyClass { public string Name { set; get; } public string Description { set; get; } public MyClass(string _name, string _des) { Name = _name; Description = _des; } } }- Proposed As Answer by Tao Liang Wednesday, April 08, 2009 6:12 AM
-
Wednesday, April 08, 2009 4:30 PMThe button's isEnabled is tied to a command, and the command's CanExecute changes dynamically. Will the loaded event fire every time isEnabled changes on the button?

