Click here to Skip to main content
15,896,915 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I am struggling with the one issue. I want to make Button enable who is set to Top.
And Rest Button i want to make disable and My Button Inside DataTemplate and That
Data template inside ComboBox.
My Code :
XML
<StackPanel VerticalAlignment="Bottom" HorizontalAlignment="Left" Orientation="Horizontal" Name="interuptStackPanel">
	<Separator Width="20"  Style="{DynamicResource VerticalSeparatorStyle}" HorizontalAlignment="Right" />

	<ComboBox x:Name="cmbInterruptButtonCombo"
              HorizontalContentAlignment="Left"
			  Visibility="Collapsed"
			  VerticalContentAlignment="Center"
			  IsEditable="True"
			  Height="35" Width="150"
			  FontSize="15" FontWeight="SemiBold"
			  SelectionChanged="cmbInterruptButtonCombo_SelectionChanged">
		<combobox.itemcontainerstyle>
			<Setter Property="Height" Value="38" />
			<Setter Property="Width" Value="140" />
			<combobox.itemtemplate>
				<datatemplate>
 <DataTemplate>
                                <Button Content = "{Binding FieldName}" Uid="{Binding FieldID}" Height = "36" Width = "130" Click = "multipleInterupt_Click" IsEnabled="False"/>
                            </DataTemplate>
                        </ComboBox.ItemTemplate>
                    </ComboBox>
i tried so many codes but all are worth less. Please Provide me solution. 
Assume that there are more than 1 button and i want to make top button enable and rest disable.
<pre lang="c#">btnFieldID++;
setItemSourceForInterruptButton.Add(new PetGalaxy_BOL.Model.InteruptModelClass.InterruptCombo
{
	FieldID = btnFieldID,
	FieldName = TbcontentName.Text
});

cmbInterruptButtonCombo.ItemsSource = setItemSourceForInterruptButton.OrderByDescending(sc => sc.FieldID);
cmbInterruptButtonCombo.Items.Refresh();


What I have tried:

Here dt not returing Buttons
C#
cmbInterruptButtonCombo.SelectedValue = 0;
DataTemplate dt = cmbInterruptButtonCombo.ItemTemplate;
Posted
Updated 7-Sep-17 2:04am
v3
Comments
Graeme_Grant 7-Sep-17 7:01am    
Do you have a screenshot of what you are trying to achieve?

Also, is this a code-behind or MVVM project?
Member 12069642 7-Sep-17 7:08am    
Yes i have. But how to paste here. i tried to paste but i can't
Graeme_Grant 7-Sep-17 7:18am    
Your incomplete code is very unclear of what you are trying to achieve... Does each item have a button or the button is for the combobox itself? Paste a link to the image.
Member 12069642 7-Sep-17 7:37am    
https://ibb.co/ihhaGv
Member 12069642 7-Sep-17 7:09am    
And This is only Code behind i don't use MVVM Pattern

1 solution

Here is a solution for you: Windows Disable ComboBoxItem in WPF ComboBox Control using Converter sample in C# for Visual Studio 2010[^]

UPDATE: I normally only do MVVM, so here is an adaption that will enable you to selectively disable individual items. All that you need to do is style the items.

1. Notification base so that code-behind changed to item enabled state will be reflected in the UI:
C#
public abstract class ObservableBase : INotifyPropertyChanged
{
    public void Set<TValue>(ref TValue field, TValue newValue,
                            [CallerMemberName] string propertyName = "")
    {
        if (EqualityComparer<TValue>.Default
                                    .Equals(field, default(TValue))
                                         || !field.Equals(newValue))
        {
            field = newValue;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

2. Next we need a class to hold the value of the ComboBoxItem and the IsEnabled state:
C#
public class Widget : ObservableBase
{
    private bool isEnabled = true;
    public bool IsEnabled
    {
        get => isEnabled;
        set => Set(ref isEnabled, value);
    }

    private string text;
    public string Text
    {
        get => text;
        set => Set(ref text, value);
    }
}

3. Now wire up the code-behind:
C#
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        InitData();
        DataContext = this;
    }

    private void InitData()
    {
        Items = new ObservableCollection<Widget>
        {
            new Widget{ Text="Widget1" },
            new Widget{ Text="Widget2", IsEnabled = false },
            new Widget{ Text="Widget3", IsEnabled = false },
            new Widget{ Text="Widget4", IsEnabled = false },
            new Widget{ Text="Widget5" }
        };
    }

    public ObservableCollection<Widget> Items { get; set; }

    private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        Debugger.Break();
    }
}

4. Now we can do the UI:
XML
<Window.Resources>
    <Style x:Key="ComboBoxItemStyle1" TargetType="{x:Type ComboBoxItem}">
        <Setter Property="IsEnabled" Value="{Binding IsEnabled, FallbackValue=true}"/>
    </Style>
</Window.Resources>

<Grid>
    <ComboBox ItemsSource="{Binding Items}"
                DisplayMemberPath="Text"
                ItemContainerStyle="{DynamicResource ComboBoxItemStyle1}"
                SelectionChanged="ComboBox_SelectionChanged"
                HorizontalAlignment="Center"
                VerticalAlignment="Center"
                Width="200"/>
</Grid>
 
Share this answer
 
v2

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