2017-06-01 175 views
4

在WPF中,如何将颜色绑定到背景色,比如viewmodel属性,会有点混乱。如何在Avalonia中绑定颜色

是否有其他方法可以绑定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> 

阿瓦隆尼亚视图模型

public class MainWindowViewModel 
{ 
    private IBrush _textbox3Foreground; 

    public IBrush Textbox3Foreground 
    { 
     get { return _textbox3Foreground; } 
     set 
     { 
      this.RaiseAndSetIfChanged(ref _textbox3Foreground, value); 
     } 
    }  

    public MainWindowViewModel() 
    { 
     Textbox3Foreground = Brushes.DarkOliveGreen;    
    } 
} 

回答

2

确保您在窗口的DataContext设为您的视图模型类的一个实例:

<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属性。

+0

谢谢,我明白了。我只是在玩阿瓦隆尼亚,它很棒! – EinApfelBaum