将库中的数据绑定到 XAML
Binding data from a library to XAML
我正在开发一个使用 ModernWPF 的项目。我想将 TitleBar.Background
绑定到 GetBG。
这是我的代码:
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>
编辑,我重命名了 GetBG 并在 MainWindow.cs 文件中将其设为 public:
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"); }
}
谢谢。
要使绑定正常工作,它需要是一个依赖项 属性,我相信它应该设置画笔而不是颜色。这是可能有效的依赖项 属性 的可能实现;在构造函数中,可以使用类似于原始 GetBG() 中的代码来确定正确的值,并设置依赖项的值 属性.
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);
}
我正在开发一个使用 ModernWPF 的项目。我想将 TitleBar.Background
绑定到 GetBG。
这是我的代码:
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>
编辑,我重命名了 GetBG 并在 MainWindow.cs 文件中将其设为 public:
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"); }
}
谢谢。
要使绑定正常工作,它需要是一个依赖项 属性,我相信它应该设置画笔而不是颜色。这是可能有效的依赖项 属性 的可能实现;在构造函数中,可以使用类似于原始 GetBG() 中的代码来确定正确的值,并设置依赖项的值 属性.
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);
}