2017-06-01 176 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