Search code examples
wpfxamlcombobox

WPF ComboBox IsEnable when using custom DataTemplate


Here it's the following. I want to disable a combo box.

In some scenarios in a window I just set SelectedItem from ViewModel to something and don't allowing the user to change it. But in some cases I want to allow.

This one works perfectly when changing ComboBoxIsEnabled property in VM.

<ComboBox ItemsSource="{Binding MyColletionView, Mode=OneWay}"
          SelectedItem="{Binding MySelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
          DisplayMemberPath="Name"
          IsEnabled="{Binding MyComboBoxIsEnabled}"/>

After changing ComboBox.ItemTemplate the IsEnable property not reacting anymore. In short, I can't disable the ComboBox.

<ComboBox ItemsSource="{Binding MyColletionView, Mode=OneWay}"
          SelectedItem="{Binding MySelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
          IsEnabled="{Binding MyComboBoxIsEnabled}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Converter={StaticResource CustomerConverter}}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

I don't want to disable the ComboBoxItem, I want to disable the ComboBox itsef.

Any suggestions, or someone facing the same issue?


Solution

  • <ComboBox Height="20" Width="200" IsEnabled="True" Margin="38,38,1682,1022" ItemsSource="{Binding SourceData}">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding}"/>
                </DataTemplate>
            </ComboBox.ItemTemplate>
    </ComboBox>
    

    I write a sample as you described.but I didnot use mvvm.I want to prove that there is no relationship in IsEnable and ItemTemplate. you can trace this issue from tips as follow.

    1. Watch Output window in VisualStudio.There may be some exception.
    2. replace mvvm with codebehind and try.