Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将组合框的可见性绑定到多个属性_C#_Wpf_Xaml_Combobox - Fatal编程技术网

C# 将组合框的可见性绑定到多个属性

C# 将组合框的可见性绑定到多个属性,c#,wpf,xaml,combobox,C#,Wpf,Xaml,Combobox,假设我有一个名为MainWindow.xaml的窗口和一个名为Page.xaml的页面 在MainWindow.xaml上: 我有一个菜单和一个框架。在此框架中,将显示Page.xaml MainWindow的DataContext。xaml绑定到MainWindowViewModel 我在MainWindowViewModel上有一个名为SelectedMenuItem的属性,类型为int 因此,菜单的SelectedItem绑定到SelectedMenuItem 在Page.xaml上: 我

假设我有一个名为MainWindow.xaml的窗口和一个名为Page.xaml的页面

在MainWindow.xaml上:

我有一个
菜单
和一个
框架
。在此框架中,将显示Page.xaml

MainWindow的
DataContext
。xaml
绑定到
MainWindowViewModel

我在
MainWindowViewModel
上有一个名为
SelectedMenuItem
的属性,类型为
int

因此,菜单的SelectedItem绑定到
SelectedMenuItem

在Page.xaml上:

我有两个名为
Combo1
Combo2
combobox

现在我想将
Combo2
Visibility
绑定到某个东西

所需的行为:

当选择了
Combo1的第一项&&SelectedMenuItem=1时,Combo2应该可见

那么,我应该如何绑定Combo2的可见性呢

更新:

根据您的回答,我尝试实现IMultiValueConverter,如下代码所示:

EffectsVisibilityConverter.cs

public class EffectsVisibilityConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (!(values[0] == null || values[1] == null))
        {
            if (((int)values[0] == 1) && ((int)values[1] == 1))
            {
                return Visibility.Visible;
            }
            else
            {
                return Visibility.Collapsed;
            }
        }
        else
        {
            return Visibility.Collapsed;
        }
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        return null;
    }
}
在Page.xaml中:

<ComboBox ItemsSource="{Binding DataContext.Effects, RelativeSource={RelativeSource AncestorType={x:Type Page}}}"  
          DisplayMemberPath="Effect" SelectedValue="{Binding EffectID}" SelectedValuePath="EffectID">
    <ComboBox.Visibility>
        <MultiBinding Converter="{StaticResource effectsVisibilityConverter}">
            <Binding Path="SelectedParent" >
                <Binding.Source>
                    <vm:MainWindowViewModel />
                </Binding.Source>
            </Binding>
            <Binding Path="DataContext.SelectedGroupID" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type Page}}" />
        </MultiBinding>
    </ComboBox.Visibility>
</ComboBox>

但问题是,转换器中值[0]的值始终保持为0。

您可以使用和绑定,例如SelectedMenuItem和Combo1。SelectedIndex到转换器来确定Combo2的可见性

我更喜欢这种方式,而不是创建新的viewmodel属性,因为它是纯ui逻辑,与viewmodels逻辑无关

//编辑

你为什么不像库纳写的那样使用绑定

<MultiBinding Converter="{StaticResource NameConverter}">
    <Binding Path="SelectedItem" ElementName="cbox1"/>
    <Binding Path="IsChecked" ElementName="chkbox"/>
</MultiBinding>

只需使用SelectedIndex而不是SelectedItem,一切都很好。

您可以使用和绑定,例如SelectedMenuItem和Combo1。SelectedIndex到转换器来确定Combo2的可见性

我更喜欢这种方式,而不是创建新的viewmodel属性,因为它是纯ui逻辑,与viewmodels逻辑无关

//编辑

你为什么不像库纳写的那样使用绑定

<MultiBinding Converter="{StaticResource NameConverter}">
    <Binding Path="SelectedItem" ElementName="cbox1"/>
    <Binding Path="IsChecked" ElementName="chkbox"/>
</MultiBinding>

只需使用SelectedIndex而不是SelectedItem,一切都很好。

您可以使用和绑定,例如SelectedMenuItem和Combo1。SelectedIndex到转换器来确定Combo2的可见性

我更喜欢这种方式,而不是创建新的viewmodel属性,因为它是纯ui逻辑,与viewmodels逻辑无关

//编辑

你为什么不像库纳写的那样使用绑定

<MultiBinding Converter="{StaticResource NameConverter}">
    <Binding Path="SelectedItem" ElementName="cbox1"/>
    <Binding Path="IsChecked" ElementName="chkbox"/>
</MultiBinding>

只需使用SelectedIndex而不是SelectedItem,一切都很好。

您可以使用和绑定,例如SelectedMenuItem和Combo1。SelectedIndex到转换器来确定Combo2的可见性

我更喜欢这种方式,而不是创建新的viewmodel属性,因为它是纯ui逻辑,与viewmodels逻辑无关

//编辑

你为什么不像库纳写的那样使用绑定

<MultiBinding Converter="{StaticResource NameConverter}">
    <Binding Path="SelectedItem" ElementName="cbox1"/>
    <Binding Path="IsChecked" ElementName="chkbox"/>
</MultiBinding>


只需使用SelectedIndex而不是SelectedItem,一切都很好。

只需在viewmodel中添加另一个属性并执行其中的逻辑即可

只需在viewmodel中添加另一个属性,并在其中执行逻辑

只需在viewmodel中添加另一个属性,并在其中执行逻辑

只需在viewmodel中添加另一个属性,并在其中执行逻辑

使用多重绑定和转换器:我使用了复选框的
IsChecked

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:NameConverter x:Key="NameConverter"/>
    </Window.Resources>
    <Grid>

        <ComboBox x:Name="cbox1" VerticalAlignment="Center" Text="3"   MinHeight="20">
            <ComboBoxItem  Content="first" />
            <ComboBoxItem  Content="second"/>
            <ComboBoxItem  Content="third"/>
            <ComboBoxItem  Content="fourth"/>
        </ComboBox>

        <ComboBox VerticalAlignment="Bottom" Text="3"   MinHeight="20" >
            <ComboBox.Visibility>
                <MultiBinding Converter="{StaticResource NameConverter}">
                    <Binding Path="SelectedItem" ElementName="cbox1"/>
                    <Binding Path="IsChecked" ElementName="chkbox"/>
                </MultiBinding>
            </ComboBox.Visibility>
            <ComboBoxItem  Content="1" />
            <ComboBoxItem  Content="2"/>
            <ComboBoxItem  Content="3"/>
            <ComboBoxItem  Content="4"/>
            </ComboBox>
        <CheckBox x:Name="chkbox" Content="Isselected" HorizontalAlignment="Left" Margin="10,56,0,0" VerticalAlignment="Top"/>

    </Grid>
</Window>



public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
}

public class NameConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if ((values[0] is ComboBoxItem) && (values[0] as ComboBoxItem).Content.ToString() == "first" && (bool)values[1])

            return Visibility.Visible;
        else return Visibility.Collapsed;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        return null;
    }
}

公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
}
}
公共类名称转换器:IMultiValueConverter
{
公共对象转换(对象[]值,类型targetType,对象参数,CultureInfo区域性)
{
如果((值[0]为ComboBoxItem)和((值[0]为ComboBoxItem)。Content.ToString()=“first”&(bool)值[1])
返回可见性。可见;
否则返回可见性。崩溃;
}
公共对象[]转换回(对象值,类型[]目标类型,对象参数,CultureInfo区域性)
{
返回null;
}
}
更新//

<ComboBox.Visibility>
    <MultiBinding Converter="{StaticResource NameConverter}">
        <Binding Path="SelectedItem" ElementName="cbox1"/>
        <Binding  Path="IsChecked">
            <Binding.RelativeSource >
                <RelativeSource Mode="FindAncestor" AncestorType="{x:Type Window}" />
            </Binding.RelativeSource>

        </Binding>
    </MultiBinding>
</ComboBox.Visibility>

使用多重绑定和转换器:我使用了复选框的
IsChecked而不是menuitem

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:NameConverter x:Key="NameConverter"/>
    </Window.Resources>
    <Grid>

        <ComboBox x:Name="cbox1" VerticalAlignment="Center" Text="3"   MinHeight="20">
            <ComboBoxItem  Content="first" />
            <ComboBoxItem  Content="second"/>
            <ComboBoxItem  Content="third"/>
            <ComboBoxItem  Content="fourth"/>
        </ComboBox>

        <ComboBox VerticalAlignment="Bottom" Text="3"   MinHeight="20" >
            <ComboBox.Visibility>
                <MultiBinding Converter="{StaticResource NameConverter}">
                    <Binding Path="SelectedItem" ElementName="cbox1"/>
                    <Binding Path="IsChecked" ElementName="chkbox"/>
                </MultiBinding>
            </ComboBox.Visibility>
            <ComboBoxItem  Content="1" />
            <ComboBoxItem  Content="2"/>
            <ComboBoxItem  Content="3"/>
            <ComboBoxItem  Content="4"/>
            </ComboBox>
        <CheckBox x:Name="chkbox" Content="Isselected" HorizontalAlignment="Left" Margin="10,56,0,0" VerticalAlignment="Top"/>

    </Grid>
</Window>



public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
}

public class NameConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if ((values[0] is ComboBoxItem) && (values[0] as ComboBoxItem).Content.ToString() == "first" && (bool)values[1])

            return Visibility.Visible;
        else return Visibility.Collapsed;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        return null;
    }
}

公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
}
}
公共类名称转换器:IMultiValueConverter
{
公共对象转换(对象[]值,类型targetType,对象参数,CultureInfo区域性)
{
如果((值[0]为ComboBoxItem)和((值[0]为ComboBoxItem)。Content.ToString()=“first”&(bool)值[1])
返回可见性。可见;
否则返回可见性。崩溃;
}
公共对象[]转换回(对象值,类型[]目标类型,对象参数,CultureInfo区域性)
{
返回null;
}
}
更新//

<ComboBox.Visibility>
    <MultiBinding Converter="{StaticResource NameConverter}">
        <Binding Path="SelectedItem" ElementName="cbox1"/>
        <Binding  Path="IsChecked">
            <Binding.RelativeSource >
                <RelativeSource Mode="FindAncestor" AncestorType="{x:Type Window}" />
            </Binding.RelativeSource>

        </Binding>
    </MultiBinding>
</ComboBox.Visibility>

使用多重绑定和转换器:我使用了复选框的
IsChecked而不是menuitem

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:NameConverter x:Key="NameConverter"/>
    </Window.Resources>
    <Grid>

        <ComboBox x:Name="cbox1" VerticalAlignment="Center" Text="3"   MinHeight="20">
            <ComboBoxItem  Content="first" />
            <ComboBoxItem  Content="second"/>
            <ComboBoxItem  Content="third"/>
            <ComboBoxItem  Content="fourth"/>
        </ComboBox>

        <ComboBox VerticalAlignment="Bottom" Text="3"   MinHeight="20" >
            <ComboBox.Visibility>
                <MultiBinding Converter="{StaticResource NameConverter}">
                    <Binding Path="SelectedItem" ElementName="cbox1"/>
                    <Binding Path="IsChecked" ElementName="chkbox"/>
                </MultiBinding>
            </ComboBox.Visibility>
            <ComboBoxItem  Content="1" />
            <ComboBoxItem  Content="2"/>
            <ComboBoxItem  Content="3"/>
            <ComboBoxItem  Content="4"/>
            </ComboBox>
        <CheckBox x:Name="chkbox" Content="Isselected" HorizontalAlignment="Left" Margin="10,56,0,0" VerticalAlignment="Top"/>

    </Grid>
</Window>



public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
}

public class NameConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if ((values[0] is ComboBoxItem) && (values[0] as ComboBoxItem).Content.ToString() == "first" && (bool)values[1])

            return Visibility.Visible;
        else return Visibility.Collapsed;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        return null;
    }
}