如何在 Avalonia 中绑定颜色
How to bind color in Avalonia
在 WPF 中,如何绑定颜色(如背景颜色到视图模型)有点令人困惑 属性。
在 Avalonia 中还有其他绑定颜色的方法吗?
或者这个例子是个好方法吗?
阿瓦隆尼亚景观
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Button.Views.MainWindow"
Title="Button" Width="700">
<StackPanel Grid.Column="2" Orientation="Vertical" Gap="8" Margin="10">
<TextBox Name="Textbox3" Text="{Binding Textbox3Text}" Foreground="{Binding Textbox3Foreground}"/>
</StackPanel>
</Window>
Avalonia ViewModel
public class MainWindowViewModel
{
private IBrush _textbox3Foreground;
public IBrush Textbox3Foreground
{
get { return _textbox3Foreground; }
set
{
this.RaiseAndSetIfChanged(ref _textbox3Foreground, value);
}
}
public MainWindowViewModel()
{
Textbox3Foreground = Brushes.DarkOliveGreen;
}
}
确保已将 window 的 DataContext
设置为视图模型的实例 class:
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Button.Views.MainWindow"
Title="Button" Width="700">
<Window.DataContext>
<local:MainWindowViewModel />
</Window.DataContext>
<StackPanel Grid.Column="2" Orientation="Vertical" Gap="8" Margin="10">
<TextBox Name="Textbox3" Text="{Binding Textbox3Text}" Foreground="{Binding Textbox3Foreground}"/>
</StackPanel>
</Window>
一般来说,您通常不会在视图模型中定义 UI 相关的东西,例如颜色。这些东西通常直接在视图中定义,没有任何绑定。但是你当然可以像这样绑定到 Brush
属性。
在 WPF 中,如何绑定颜色(如背景颜色到视图模型)有点令人困惑 属性。
在 Avalonia 中还有其他绑定颜色的方法吗?
或者这个例子是个好方法吗?
阿瓦隆尼亚景观
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Button.Views.MainWindow"
Title="Button" Width="700">
<StackPanel Grid.Column="2" Orientation="Vertical" Gap="8" Margin="10">
<TextBox Name="Textbox3" Text="{Binding Textbox3Text}" Foreground="{Binding Textbox3Foreground}"/>
</StackPanel>
</Window>
Avalonia ViewModel
public class MainWindowViewModel
{
private IBrush _textbox3Foreground;
public IBrush Textbox3Foreground
{
get { return _textbox3Foreground; }
set
{
this.RaiseAndSetIfChanged(ref _textbox3Foreground, value);
}
}
public MainWindowViewModel()
{
Textbox3Foreground = Brushes.DarkOliveGreen;
}
}
确保已将 window 的 DataContext
设置为视图模型的实例 class:
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Button.Views.MainWindow"
Title="Button" Width="700">
<Window.DataContext>
<local:MainWindowViewModel />
</Window.DataContext>
<StackPanel Grid.Column="2" Orientation="Vertical" Gap="8" Margin="10">
<TextBox Name="Textbox3" Text="{Binding Textbox3Text}" Foreground="{Binding Textbox3Foreground}"/>
</StackPanel>
</Window>
一般来说,您通常不会在视图模型中定义 UI 相关的东西,例如颜色。这些东西通常直接在视图中定义,没有任何绑定。但是你当然可以像这样绑定到 Brush
属性。