wpf组合框多重绑定在第一步中无法获得相对源组合框的组合框项目(以编程方式添加或绑定到数据库)

问题描述 投票:0回答:1

我想通过特定条件禁用组合框的某些项目。对于此问题,我使用了多重绑定。如果我在xaml中描述了combobox的所有项目,那就没有问题。但是我想以编程方式填充组合框项目。因此,在这种情况下,我无法获得项目,返回null并在第一步将我扔出程序。我的xaml代码是这样的:

<Window.Resources>
    <local:TekerDisabler x:Key="tekerDisabler"/>
</Window.Resources>
<Grid>
    <ComboBox x:Name="cbx" HorizontalAlignment="Left" Margin="41,125,0,0" VerticalAlignment="Top" Width="227">
        <ComboBox.ItemContainerStyle>
            <Style TargetType="ComboBoxItem">
                <Setter Property="IsEnabled">
                    <Setter.Value>
                        <MultiBinding Converter="{StaticResource tekerDisabler}">
                            <Binding ElementName="txt1" Path="Text"/>
                            <Binding ElementName="txt2" Path="Text"/>
                            <Binding ElementName="txt3" Path="Text"/>

                            <Binding RelativeSource="{RelativeSource Self}"/>
                        </MultiBinding>
                    </Setter.Value>
                </Setter>
            </Style>
        </ComboBox.ItemContainerStyle>
    </ComboBox>
    <TextBox x:Name="txt1" HorizontalAlignment="Left" Height="23" Margin="41,38,0,0" TextWrapping="Wrap"  VerticalAlignment="Top" Width="120"/>
    <TextBox x:Name="txt2" HorizontalAlignment="Left" Height="23" Margin="207,38,0,0" TextWrapping="Wrap"  VerticalAlignment="Top" Width="211" TextChanged="txt2_TextChanged"/>
    <TextBox x:Name="txt3" HorizontalAlignment="Left" Height="24" Margin="478,37,0,0" TextWrapping="Wrap"  VerticalAlignment="Top" Width="196"/>

</Grid>

和我的C#代码是这样的:

namespace App1.Pencereler
{

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

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        cbx.Items.Add("0");
        cbx.Items.Add("1");
        cbx.Items.Add("2");
        cbx.Items.Add("3");
    }

    private void txt2_TextChanged(object sender, TextChangedEventArgs e)
    {
        cbx.SelectedIndex = 1;
    }
}
class TekerDisabler : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        bool enable = true;
        var itemler = values[3] as ComboBoxItem;
        if (itemler == null || values[0].ToString() == null || values[1].ToString() == null || values[2].ToString() == null)
        { enable = true; }
        else
        {
            switch (values[0].ToString())
            {
                case "a":
                    switch (values[1].ToString())
                    {
                        case "b":
                            switch (values[2].ToString())
                            {
                                case "c":
                                    switch (itemler.Content.ToString())
                                    {
                                        case "0":
                                        case "2":
                                            enable = false;
                                            break;
                                        default:
                                            enable = true;
                                            break;
                                    }
                                    break;
                                default:
                                    enable = true;
                                    break;
                            }
                            break;
                        default:
                            enable = true;
                            break;
                    }
                    break;
                default:
                    enable = true;
                    break;
            }

        }
        return enable;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

}

例如,在第一步中,我写txt1:a,txt2:b,txt3:d,这样所有项目的显示都启用,然后我写txt1:a,txt2:b,txt3:c和组合框的内容(0,2)禁用,没有问题。但是,当运行程序时,在第一步中,当我下拉组合框时,我会写txt1:a,txt2:b,txt3:c,程序将我赶出去。如何克服这个问题?

wpf combobox multibinding
1个回答
0
投票

知道您将得到什么确切的错误会很有趣。我假设ComboBoxItem.Content返回null

无论如何,以下简化的代码版本很可能会解决您的问题:

TekerDisabler.cs

public class TekerDisabler : IMultiValueConverter
{
  #region Implementation of IMultiValueConverter

  public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
  {
    var currentItem = values[0] as string;
    var predicate = "abc";
    string input = string.Concat(values.Cast<string>().Skip(1));
    return !(input.Equals(predicate, StringComparison.Ordinal)
             && (currentItem.Equals("0", StringComparison.Ordinal)
                 || currentItem.Equals("2", StringComparison.Ordinal)));
  }

  public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) =>
    throw new NotSupportedException();

  #endregion
}

MainWindow.xaml

<ComboBox>
  <ComboBox.ItemContainerStyle>
    <Style TargetType="ComboBoxItem">
      <Setter Property="IsEnabled">
        <Setter.Value>
          <MultiBinding Converter="{StaticResource CellForegroundMultiValueConverter}">
            <Binding />
            <Binding ElementName="TextBox1" Path="Text" />
            <Binding ElementName="TextBox2" Path="Text" />
            <Binding ElementName="TextBox3" Path="Text" />
          </MultiBinding>
        </Setter.Value>
      </Setter>
    </Style>
  </ComboBox.ItemContainerStyle>
</ComboBox>
<TextBox x:Name="TextBox1" />
<TextBox x:Name="TextBox2" />
<TextBox x:Name="TextBox3" />
© www.soinside.com 2019 - 2024. All rights reserved.