多重绑定,转换器和设置器:意外错误

时间:2018-11-20 09:05:47

标签: c# wpf binding multibinding

我正在尝试为WPF中的按钮使用多重绑定样式,但出现一个尴尬的错误: System.ArgumentException:``System.Drawing.SolidBrush'n'est pas une vale de placeinproprieté'System.Windows.Controls.Panel.Background'd'uneméthodeSetter。'

但是我正在将Setter应用于Button.BackgroundProperty ...

这是多重绑定:

    <Button
                            Height="20"
                            Margin="2,0,2,2"
                            VerticalAlignment="Bottom"
                            Click="Btn_summary_OnClick"
                            Content="Résumé"
                            Name="btn_summary">
                            <Button.Style>
                                <MultiBinding Converter="{StaticResource StyleConverter1}">
                                    <Binding ElementName="listBoxBooks" Path="SelectedItem" />
                                    <Binding Source="{StaticResource bookManagement}" Path="SelectedTab" Mode="OneWay"/>
                                    <Binding RelativeSource="{RelativeSource Self}" Path="Name"/>
                                </MultiBinding>
                            </Button.Style>
                        </Button>

和转换器:

    public class StyleConverter1 : IMultiValueConverter
    {

        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            Style styleToApply = new Style(typeof(Button));

            Object selectedItem = values[0];
            if (selectedItem == null)
            {
                styleToApply.Setters.Add(new Setter(Button.IsEnabledProperty, false));
                return styleToApply;
            }




            styleToApply.Setters.Add(new Setter(Button.IsEnabledProperty, true));

            string selectedTab = values[1] as string;
            if (selectedTab == null)
            {
                return styleToApply;
            }


            string buttonName = values[2] as string;
            if ((selectedTab.Equals("summary") && buttonName.Equals("btn_summary"))
                || (selectedTab.Equals("end") && buttonName.Equals("btn_end"))
                || (selectedTab.Equals("amazon") && buttonName.Equals("btn_amazon"))
                )
            {
                styleToApply.Setters.Add(new Setter(Button.BackgroundProperty,Brushes.Yellow));
                styleToApply.Setters.Add(new Setter(Button.ForegroundProperty, Brushes.RoyalBlue));
            }
            else
            {
                styleToApply.Setters.Add(new Setter(Button.ForegroundProperty, Brushes.Yellow));
                styleToApply.Setters.Add(new Setter(Button.BackgroundProperty, Brushes.RoyalBlue));
            }

            return styleToApply;
        }

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


    }

我不明白为什么VS想要将我的设置者应用于面板。

谢谢

1 个答案:

答案 0 :(得分:3)

System.Drawing是Winforms的命名空间。删除它并替换为WPF等效的System.Windows.Media,然后将使用正确的Brushes类。