簡體   English   中英

UWP用戶控制ConverterParameter綁定為null

[英]UWP User Control ConverterParameter Binding is null

我有一個具有屬性ThemeUserControl Theme只是ElementTheme一個Enum

public sealed partial class PlaylistControl : UserControl, MediaControlListener
{
    public ElementTheme Theme { get; set; }
    public static readonly DependencyProperty ThemeProperty = DependencyProperty.Register("Theme", typeof(ElementTheme), typeof(PlaylistControl), new PropertyMetadata(null));

    ...
}

在該控件的xaml中,我有一個TextBlockForegroundForeground="{x:Bind IsPlaying, Converter={StaticResource RowColorConverter}, ConverterParameter={Binding Theme}, Mode=OneWay}"

但是,轉換器似乎沒有得到主題。 始終為空。

class RowColorConverter : Windows.UI.Xaml.Data.IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        return value.Equals(true) ? Helper.GetHighlightBrush() :
                                    parameter is ElementTheme && (ElementTheme)parameter == ElementTheme.Dark ? Helper.WhiteSmokeBrush : Helper.BlackBrush;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        return null;
    }
}

怎么了?

創建主題時使用了new PropertyMetadata(null)

其含義是將Theme的默認值設置為null 請嘗試將其修改為new PropertyMetadata(ElementTheme.Light)

更新資料

當播放音樂時,我不知道該轉換回哪種顏色,所以我正在嘗試這種方式

假設您不需要主題資源,我會嘗試簡化內容。 這是該想法的解決方案:

首先在App.xaml中定義兩個畫筆資源,分別是未播放狀態和播放狀態。

<SolidColorBrush x:Key="UnplayForeground" Color="White"/>
<SolidColorBrush x:Key="PlayingForeground" Color="Red"/>

編寫一種方法以從當前Resource資源中檢索具有指定名稱的資源:

public static Brush GetResourceBrush(string key)
{
    return (Brush)Application.Current.Resources[key];
}

然后直接使用IsPlaying屬性將其轉換。

public class RowColorConverter:IValueConverter
{
    if(value is bool isPlaying)
    {
        return isPlaying ? GetResourceBrush("PlayingForeground") : 
                           GetResourceBrush("UnplayForeground");
    }
    return GetResourceBrush("UnplayForeground");
}

更新2

但是您的密鑰與我的主題有何不同?

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.ThemeDictionaries>
            <ResourceDictionary x:Key="Light">
                <SolidColorBrush x:Key="PlayingForeground" Color="Green"/>
                <SolidColorBrush x:Key="UnplayForeground" Color="Black"/>
            </ResourceDictionary>
            <ResourceDictionary x:Key="Dark">
                <SolidColorBrush x:Key="PlayingForeground" Color="Red"/>
                <SolidColorBrush x:Key="UnplayForeground" Color="White"/>
            </ResourceDictionary>
        </ResourceDictionary.ThemeDictionaries>
    </ResourceDictionary>
</Application.Resources>

主題開關需要創建兩個分別名為Light和Dark的資源字典。 在這兩個資源詞典中,存在具有相同名稱的資源。

當需要更改主題時,應修改控件的RequestedTheme屬性,而不是創建新的Theme屬性。

切換主題時,軟件還會切換相應的ResourceDictionary ,並且ResourceDictionary名稱不變。 這就是為什么GetResourceBrush(string key)始終基於key獲取當前主題資源的原因。

最好的祝福。

感謝理查德回答我的問題。 正如他提到的那樣,我無法將綁定的主題傳遞給ConverterParameter 因此,沒有辦法使用ConverterParameter解決我的問題。

我發布答案的原因是我只想顯示解決方法以解決問題。

由於我僅同時顯示一個PlaylistControl ,因此可以聲明另一個靜態變量UITheme CurrentTheme 控件Loaded ,將其成員變量Theme分配給CurrentTheme ,我可以輕松地在轉換器中切換顏色:

public object Convert(object value, Type targetType, object parameter, string language)
{
    return value.Equals(true) ? Helper.GetHighlightBrush() :
                                CurrentTheme == ElementTheme.Dark ? Helper.WhiteSmokeBrush : Helper.BlackBrush;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM