开发者

ComboBox IsEnabled binding in a DataForm not working

I have several ComboBoxes in a DataForm and am trying to have it so that certain ComboBoxes are disabled until a particular ComboBox is selected. To this end, I created a notification property named CanEditCombo in the class that is bound to the DataContext and configured my ComboBoxes like so:

<ComboBox ... IsEnabled="{Binding CanEditCombo, Mode=OneWay}" />

The CanEditCombo is initially false, yet my ComboBoxes are editable when the DataForm first loads.

If I apply the same IsEnabled binding syntax to a TextBox in my DataForm it works as expected: disabled at first but enabled once CanEditCombo becomes true.

Here is an example chunk of XAML:

<toolkit:DataForm CurrentItem="{Binding NewProject, Mode=TwoWay}" x:Name="dfNewProject" CommandButtonsVisibility="None">
    <toolkit:DataForm.EditTemplate>
        <DataTemplate>
            <StackPanel>
                ...

                <toolkit:DataField>
                    <ComboBox开发者_Go百科 ItemsSource="{Binding ProjectOptions, Mode=OneWay}"
                                SelectedValue="{Binding Options, Mode=TwoWay}"
                                DisplayMemberPath="Value"
                                SelectedValuePath="Key"
                                IsEnabled="{Binding CanEditCombo, Mode=OneWay}" />
                </toolkit:DataField>

                ...
            </StackPanel>
        </DataTemplate>
    </toolkit:DataForm.EditTemplate>
</toolkit:DataForm>

What's more, even if I hard-code the ComboBox's IsEnabled property to False in the markup above the ComboBox is still editable.

How do I go about having the ComboBox's IsEnable property set via binding syntax when the ComboBox is in a DataForm?


Not sure if you've discovered the answer to this yet, but there's actually a really simple solution: set IsEnabled on the DataField rather than the ComboBox (or any other DataField nested control).

<toolkit:DataField IsEnabled="{Binding CanEditCombo, Mode=OneWay}">
     <ComboBox ItemsSource="{Binding ProjectOptions, Mode=OneWay}"
               SelectedValue="{Binding Options, Mode=TwoWay}"
               DisplayMemberPath="Value"
               SelectedValuePath="Key" />
</toolkit:DataField>

I'm not sure exactly what the DataField is doing internally but the IsEnabled property never gets accessed on a nested control.


The following is the simplest mockup I could make, to show that it should work. The checkbox toggles the enabled state of the combo boxes quite happily.

Please look for any differences that might explain why your is not working (or post more code & XAML)

XAML:

<UserControl x:Class="ComboDemo.ComboEnabledTest"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <StackPanel x:Name="LayoutRoot" Background="White">
        <ComboBox IsEnabled="{Binding CanEdit, Mode=OneWay}" Height="23" Width="120" />
        <ComboBox IsEnabled="{Binding CanEdit, Mode=OneWay}" Height="23" Width="120" />
        <CheckBox IsChecked="{Binding CanEdit, Mode=TwoWay}" Content="Toggle CanEnable" Height="16" />
    </StackPanel>
</UserControl>

Code behind:

using System.Windows.Controls;

namespace ComboDemo
{
    public partial class ComboEnabledTest : UserControl
    {
        public ComboEnabledTest()
        {
            InitializeComponent();
            this.DataContext = new TestViewModel();
        }
    }
}

View Model:

using System.ComponentModel;

namespace ComboDemo
{
    public class TestViewModel : INotifyPropertyChanged
    {
        private bool _canEdit;
        public bool CanEdit
        {
            get { return _canEdit; }
            set
            {
                if (_canEdit != value)
                {
                    _canEdit = value;
                    if (this.PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("CanEdit"));
                    }
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜