WPF:如何更改我的组合框项目的可见性? [英] WPF: How to change the visibility of my ComboBox Items?

查看:54
本文介绍了WPF:如何更改我的组合框项目的可见性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个话题已经回答了,但是我无法通过找到的解决方案解决问题.所以这是我的代码:

I know that this topic is already answered, but i couldn't solve the problem with the solution that i found. So here is my code:

<DataGrid HeadersVisibility="Column" Name="griglia" Grid.Row="2" ItemsSource="{Binding Path=Test}" AutoGenerateColumns="True" IsReadOnly="True" ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.HorizontalScrollBarVisibility="Visible">
        <DataGrid.ColumnHeaderStyle>
            <Style TargetType="{x:Type DataGridColumnHeader}">
                <Setter Property="ContentTemplate" >
                    <Setter.Value>
                        <DataTemplate DataType="DataGridColumnHeader"  >
                            <ComboBox SelectedValue="{Binding Selezione}" SelectedValuePath="Selezionato" Width="100" Height="20" ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}},Path=DataContext.Selezione}"  SelectionChanged="Test_SelectionChanged">
                                <ComboBox.ItemTemplate>
                                    <DataTemplate >
                                        <TextBlock Text="{Binding Path=Oggetto}"/>
                                    </DataTemplate>
                                </ComboBox.ItemTemplate>
                            </ComboBox>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </DataGrid.ColumnHeaderStyle>
    </DataGrid>

简而言之,我有一个绑定到数据表的自定义数据网格.每列的标题是一个组合框,其来源是该类的一个可观察的集合:

In few words, i have a custom datagrid which is bound to a datatable. The header of each column, is a combobox, whose source is an observable collection of this class:

public class PopolazioneCombo 
{
    public string Oggetto { get; set; }
    private bool selezionato = false;
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string propName)
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propName));
        }
    }
    public bool Selezionato
    {
        get { return !selezionato; }
        set
        {
            if(selezionato != value)
            {
                selezionato = value;
                OnPropertyChanged("Selezionato");
            }
        }
    }
}

我的问题如下:我需要将每个选定项目的可见性切换到所有组合框中的折叠状态(因为它们共享相同的源).我试图创建一个自定义的SelectionChanged事件,在该事件中我更改了"Selezionato"值,并限制了文本块的可见性,如下所示:

My problem is the following: I need to switch the visibility of each selected item to collapsed in all the comboboxes (since they share the same source). I tried to create a custom SelectionChanged event in which i change the "Selezionato" value and i bound the visibility of the textblock like this:

<TextBlock Text="{Binding Path=Oggetto}" Visibility="{Binding Path=Selezionato, Converter={StaticResource BoolToVis}, UpdateSourceTrigger=PropertyChanged}"/>

这个问题是,不仅使我的SelectedItem在组合框中不可见,而且ComoBoxItem也没有同步,因此它根本无法按预期工作.

The problem with this is that, not only it make my SelectedItem not visible in the combobox, but also the ComoBoxItem are not synchronized, so it's not working at all as intended.

这是集合

 public ObservableCollection<PopolazioneCombo> Selezione
    {
        get
        {
            return selezione;
        }
        set
        {
            if (selezione != value)
            {
                selezione = value;
                OnPropertyChanged("Selezione");
            }
        }
    }

我的要求是,如果在N个组合框中的任何一个中都选择了一个项目,那么该项目将无法被任何人选择,直到他失去SelectedItem状态.例如,假设我有2个组合框和4个项目(x,y,a,b)的集合.如果在ComboBox1中选择了x,则直到ComboBox1的SelectedItem更改(例如,从x更改为y)之前,都无法在2个ComboBox中选择x.现在,我什至可以接受以下事实:如果下拉菜单中的项目使事情变得更容易,则该项目将被禁用;我只需要一个事实,即如果已经选择了该项目,则无法再次选择该项目

EDIT 2: My requirement is that, if in any of the N combobox an item is selected, then, that item cannot be selected by anyone, until he lose the SelectedItem status. For instance let's assume i have 2 combobox and a collection of 4 Item (x,y,a,b) . If x is selected in ComboBox1, then x can't be selected in none of the 2 ComboBox until the SelectedItem of the ComboBox1 change (from x to y for instance). Now i can even accept the fact that the item in the dropdown is just disabled if it makes the things easier, i just need the fact that it cannot be selected again if he is already selected

推荐答案

要求:

  1. 多个组合框都显示相同的选项集.
  2. 如果在组合框中选择了一个项目,则可能不会在任何 other 组合框中选择该项目.
  1. Multiple combo boxes all display the same set of options.
  2. If an item is selected in a combo box, it may not be selected in any other combo box.

我们将在组合框项目的bool Selezionato属性的驱动下,在组合框上的ItemContainerStyle中使用触发器进行隐藏和禁用.我们也希望在样式中也将Selezionato设置为Binding,但是我发现它有时会取消选择项目,所以我改为在ComboBox.SelectionChanged处理程序中进行了选择. .

We'll do the hiding and disabling with triggers in an ItemContainerStyle on the comboboxes, driven by the bool Selezionato property of the combo box items. We'd prefer to set Selezionato with a Binding in the style as well, but I found that it was deselecting items at times when I didn't want it to, so I did it in a ComboBox.SelectionChanged handler instead.

<Style 
    x:Key="SingleSelectionComboBoxItem" 
    TargetType="ComboBoxItem" 
    BasedOn="{StaticResource {x:Type ComboBoxItem}}"
    >
    <!-- This unselects sometimes when you don't want it to. -->
    <!--
    <Setter Property="IsSelected" Value="{Binding Selezionato, Mode=OneWayToSource}" />
    -->
    <Style.Triggers>
        <DataTrigger Binding="{Binding Selezionato}" Value="True">
            <!-- Hide it -->
            <Setter Property="Visibility" Value="Collapsed" />
            <!-- Also prevent user from selecting it via arrows or mousewheel -->
            <Setter Property="IsEnabled" Value="False" />
        </DataTrigger>
    </Style.Triggers>
</Style>

组合框.我遗漏了您拥有的一些属性.对于我添加到您的代码中重要的是ItemContainerStyleSelectionChanged:

ComboBoxes. I'm leaving out some of the properties you had. What matters for my addition to your code is ItemContainerStyle and SelectionChanged:

<ComboBox
    ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext.Selezione}" 
    DisplayMemberPath="Oggetto"
    ItemContainerStyle="{StaticResource SingleSelectionComboBoxItem}"
    SelectionChanged="SingleSelectionComboBox_SelectionChanged"
    />

后面的代码:

private void SingleSelectionComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    foreach (var item in e.RemovedItems.OfType<PopolazioneCombo>())
    {
        item.Selezionato = false;
    }

    foreach (var item in e.AddedItems.OfType<PopolazioneCombo>())
    {
        item.Selezionato = true;
    }
}


我注意到的另一件事:


Another thing I noticed along the way:

您有PopolazioneCombo.Selezionato吸气剂返回了!selezionato-可能是拼写错误,但如果不是,那是个坏主意!

You had the PopolazioneCombo.Selezionato getter returning !selezionato -- probably a typo, but if not, a bad idea!

public bool Selezionato
{
    get { return selezionato; }
    set
    {
        if (selezionato != value)
        {
            selezionato = value;
            OnPropertyChanged("Selezionato");
        }
    }
}

这篇关于WPF:如何更改我的组合框项目的可见性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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