I am trying to use a multibinding style for a button in WPF, but I get an awkward error : System.ArgumentException : ''System.Drawing.SolidBrush' n'est pas une valeur valide pour la propriété 'System.Windows.Controls.Panel.Background' d'une méthode Setter.'
Yet I am applying my setters to Button.BackgroundProperty...
here is the multibinding:
<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>
and the converter:
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();
}
}
I don't understand why VS want to apply my setters to a panel.
thank you
System.Drawing
is the namespace for Winforms. Delete this and replace with the WPF equivalent System.Windows.Media
, and you'll then use the right Brushes
class.