如何在combobox datatemplate中启用和禁用按钮 [英] How to enable and disable button in combobox datatemplate

查看:115
本文介绍了如何在combobox datatemplate中启用和禁用按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我正在努力解决这个问题。我想让按钮启用谁设置为顶部。

和休息按钮我想禁用和我的按钮在DataTemplate和那个

组合框内的数据模板。 />
我的代码:

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 :

<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

cmbInterruptButtonCombo.SelectedValue = 0;
DataTemplate dt = cmbInterruptButtonCombo.ItemTemplate;

推荐答案

以下是您的解决方案: Windows使用C#中的转换器示例在Visual Studio 2010中禁用WPF ComboBox控件中的ComboBoxItem [ ^ ]



更新:我通常只做MVVM,所以这里有一个适应性,可以让你有选择地禁用个别项目。您需要做的就是设置项目样式。



1.通知基础以便代码隐藏更改为项目启用状态将反映在UI中:

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:
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.接下来我们需要一个类来保存ComboBoxItem和IsEnabled状态的值:


2. Next we need a class to hold the value of the ComboBoxItem and the IsEnabled state:

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.现在连接代码隐藏:


3. Now wire up the code-behind:

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.现在我们可以使用UI:


4. Now we can do the UI:

<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>


这篇关于如何在combobox datatemplate中启用和禁用按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆