Home > Software design >  Binding data from a library to XAML
Binding data from a library to XAML

Time:09-17

I'm working on a project that uses ModernWPF. I want to bind the TitleBar.Background to GetBG. Here's my code:

using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ModernWpf;

namespace TestApp.Desktop
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Color GetBG()
            {
                var bc = new BrushConverter();
                if (ModernWpf.ThemeManager.GetRequestedTheme(window).ToString() == "Black" || ModernWpf.ThemeManager.GetRequestedTheme(window).ToString() == "Dark")
                {
                    return (Color)ColorConverter.ConvertFromString("#1E1E1E");
                }
                else { return (Color)ColorConverter.ConvertFromString("#E6E6E6"); }
            }
        }
    }
}
<Window x:Name="window" x:Class="OpenRelease.Desktop.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TestApp"
        mc:Ignorable="d"
        ui:ThemeManager.RequestedTheme="Light"

        <!---Here's where i want to use the binding.--->
        ui:TitleBar.Background="{GetBG}"

        ui:TitleBar.IsIconVisible="False"
        ui:ThemeManager.IsThemeAware="True"
        xmlns:ui="http://schemas.modernwpf.com/2019"
        ui:WindowHelper.UseModernWindowStyle="True"
        Title="TestApp" Height="850" Width="1260">
    <Grid>
    </Grid>

Edit, I renamed GetBG and made it public in the MainWindow.cs file:

public Color ThemeAwareBackground()
        {
            var bc = new BrushConverter();
            if (ModernWpf.ThemeManager.GetRequestedTheme(window).ToString() == "Black" || ModernWpf.ThemeManager.GetRequestedTheme(window).ToString() == "Dark")
            {
                return (Color)ColorConverter.ConvertFromString("#1E1E1E");
            }
            else { return (Color)ColorConverter.ConvertFromString("#E6E6E6"); }
        }

Thanks.

CodePudding user response:

For binding to work it needs to be a dependency property and I believe it should set a brush and not a color. Here is a possible implementation of a dependency property that may work; in the constructor the correct value can be determined using code similar to what you have in your original GetBG() and set the value of the dependency property.

public static readonly DependencyProperty ThemeAwareBackgroundBrushProperty =
    DependencyProperty.Register(
        nameof(ThemeAwareBackgroundBrush),
        typeof(SolidColorBrush),
        typeof(MainWindow),
        new PropertyMetadata(default(SolidColorBrush)));

public SolidColorBrush ThemeAwareBackgroundBrush
{
    get => (SolidColorBrush) GetValue(ThemeAwareBackgroundBrushProperty);
    set => SetValue(ThemeAwareBackgroundBrushProperty, value);
}
  • Related