从第一个组合框中选择项目时隐藏第二个ComboBox(折叠)

时间:2014-05-14 06:11:09

标签: c# wpf combobox

我创建了用户控件,其中ComboBox添加了View1

查看1: -

<UserControl x:Class="XYZ.Views.ComboBoxView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="60" d:DesignWidth="600">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="150" />
            <ColumnDefinition Width="300" />
            <ColumnDefinition Width="200" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <TextBlock Grid.Row="1" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="5" Style="{StaticResource textControlText}" Text="{Binding Label}" />
        <ComboBox Grid.Row="1" Grid.Column="1" Margin="5" SelectedItem="{Binding SelectedItem}"  SelectedValue="{Binding Value}" ItemsSource="{Binding Items}" SelectedValuePath="Value" DisplayMemberPath="Ui" SelectedIndex="{Binding SelectedIndex}" Visibility="{Binding Visible}">
            <ComboBox.ToolTip>
                <ToolTip>
                    <TextBlock Text="{Binding Help}" FlowDirection="LeftToRight" TextWrapping="Wrap" MaxWidth="400"/>
                </ToolTip>
            </ComboBox.ToolTip>
        </ComboBox>
    </Grid>
</UserControl>

我与View1ModelSelectedItemSelectedIndex列表项和ObservableCollection属性相对应Visible

View1Model: -

class XyZComboBoxViewModel
{

    private ObservableCollection<XyZListItem> _items;

    public ObservableCollection<XyZListItem> Items
    {
        get { return _items; }
        set { _items = value; }
    }

    private XyZListItem _selectedItem;

    public XyZListItem SelectedItem
    {
        get { return _selectedItem; }
        set { 
           some code here          
        }
    }

    private Enum _visible;

   public Enum Visibile
   {
    get{return _visible}
    set{return _visible=value //passing System.Windows.Visibility.Collapsed;}
    }

    private int _selectedIndex;

    public int SelectedIndex
    {
        get { return _selectedIndex; }
        set { _selectedIndex = value; }
    }
  }

我有另一个View2View2Model

View2Model中,我创建两个View1model个对象(即2个ComboBox)并将其绑定到第二个视图View2

<ItemsControl ItemsSource="{Binding Items}" IsTabStop="False"  />

两个ComboBox(列表框用户控件)显示在View2

现在,我想隐藏第二个ComboBox(可见性设置为折叠),从第一个ComboBox中选择一个项目。

在第一个ComboBox选择项目时,我遍历View2Model中的View1Model对象,并将第二个ComboBox可见属性值设置为Visibility.Collapsed

问题是我无法隐藏第二个ComboBox

请帮帮我。

2 个答案:

答案 0 :(得分:0)

您可以使用上述代码。

 private Visibility _comboboxvisibility;

   public Visibility comboboxvisibility
   {
       get { return _comboboxvisibility; }
       set { _comboboxvisibility = value; RaisePropertyChanged("comboboxvisibility"); }
   }

   private XyZListItem _selectedItem;

   public XyZListItem SelectedItem
   {
       get { return _selectedItem; }
       set {
           comboboxvisibility = Visibility.Collapsed;         
    }
   }

答案 1 :(得分:0)

    public void UpdateAttributes(ComboBox sender)
    {
        var definitions = _attributeDefinitions.ToList();

        {
            var combobox = cmbAttributeDefinition1;

            if (sender != combobox && combobox.SelectedValue != null)
                definitions.RemoveAll(
                    x => x.AttrID == (imProfileAttributeID)combobox.SelectedValue);
        }

        {
            var combobox = cmbAttributeDefinition2;

            if (sender != combobox && combobox.SelectedValue != null)
                definitions.RemoveAll(
                    x => x.AttrID == (imProfileAttributeID)combobox.SelectedValue);
        }

        {
            var combobox = cmbAttributeDefinition3;

            if (sender != combobox && combobox.SelectedValue != null)
                definitions.RemoveAll(
                    x => x.AttrID == (imProfileAttributeID)combobox.SelectedValue);
        }

        {
            var combobox = cmbAttributeDefinition4;

            if (sender != combobox && combobox.SelectedValue != null)
                definitions.RemoveAll(
                    x => x.AttrID == (imProfileAttributeID)combobox.SelectedValue);
        }


        {
            var combobox = cmbAttributeDefinition5;

            if (sender != combobox && combobox.SelectedValue != null)
                definitions.RemoveAll(
                    x => x.AttrID == (imProfileAttributeID)combobox.SelectedValue);
        }


        sender.DataSource = definitions;
        sender.DisplayMember = "Caption";
        sender.ValueMember = "AttrID";
    }

  private void cmbAttributeDefinition1_DropDown(object sender, EventArgs e)
    {
        UpdateAttributes(sender as ComboBox);
    }

    private void cmbAttributeDefinition5_DropDown(object sender, EventArgs e)
    {
        UpdateAttributes(sender as ComboBox);
    }

    private void cmbAttributeDefinition2_DropDown(object sender, EventArgs e)
    {
        UpdateAttributes(sender as ComboBox);
    }

    private void cmbAttributeDefinition3_DropDown(object sender, EventArgs e)
    {
        UpdateAttributes(sender as ComboBox);
    }

    private void cmbAttributeDefinition4_DropDown(object sender, EventArgs e)
    {
        UpdateAttributes(sender as ComboBox);
    }
相关问题