![](/img/trans.png)
[英]Is it possible to pass the targetType of a MultiBinding to the child bindings' converters?
[英]multibinding, converters and setters : unexpected error
我正在嘗試為WPF中的按鈕使用多重綁定樣式,但遇到一個尷尬的錯誤:System.ArgumentException:” System.Drawing.SolidBrush”無效地有效地保存了System.Windows.Controls。 Panel.d'une方法的二傳手。
但是我正在將設置器應用於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想要將我的設置者應用於面板。
謝謝
System.Drawing
是Winforms的命名空間。 刪除它並替換為WPF等效的System.Windows.Media
,然后使用正確的Brushes
類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.