![](/img/trans.png)
[英]How do I bind code to the TextInput event of an Avalonia TextBox?
[英]How to bind color in Avalonia
在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>
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;
}
}
確保已將窗口的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
屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.