我正在為我的應用程式開發 UI。目前我遇到了一個問題,當我將按鈕命令屬性系結到中繼命令時,顏色的背景會重置為默認值。
我的 XAML 代碼:
<Window
AllowsTransparency="True"
Height="450"
ResizeMode="NoResize"
Title="MainWindow"
Width="800"
WindowStyle="None"
mc:Ignorable="d"
x:Class="ChatApplicatie.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:ChatApplicatie"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:viewModel="clr-namespace:ChatApplicatie.MVVM.ViewModel"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Window.DataContext>
<viewModel:MainViewModel/>
</Window.DataContext>
<Border BorderThickness="5" CornerRadius="25">
<Border.BorderBrush>
<LinearGradientBrush EndPoint="1,1" StartPoint="0,0">
<GradientStop Color="{StaticResource winBorderColor1}" Offset="0.0" />
<GradientStop Color="{StaticResource winBorderColor2}" Offset="0.75" />
<GradientStop Color="{StaticResource winBorderColor3}" Offset="1.0" />
</LinearGradientBrush>
</Border.BorderBrush>
<Border.Background>
<LinearGradientBrush EndPoint="1,0" StartPoint="0,1">
<GradientStop Color="{StaticResource primaryBackColor1}" Offset="0.0" />
<GradientStop Color="{StaticResource primaryBackColor2}" Offset="0.75" />
</LinearGradientBrush>
</Border.Background>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="1*" />
<RowDefinition Height="13*" />
</Grid.RowDefinitions>
<Label
Content="Chat Applicatie"
FontSize="15"
FontWeight="Bold"
Foreground="SlateGray"
Margin="20,0" />
<StackPanel
HorizontalAlignment="Right"
Margin="0,0,15,0"
Orientation="Horizontal">
<Button
Background="Transparent"
BorderThickness="0"
Click="ButtonMinimize_Click"
Content="?"
Foreground="Gray"
Height="20"
Width="20" />
<Button
Background="Transparent"
BorderThickness="0"
Click="ButtonClose_Click"
Content="╳"
FontSize="10"
Foreground="Gray"
Height="20"
Width="20" />
</StackPanel>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="3*" />
</Grid.ColumnDefinitions>
<DockPanel Grid.Column="0">
<TextBox
DockPanel.Dock="Top"
Foreground="White"
Height="25"
Text="{Binding Username, UpdateSourceTrigger=PropertyChanged}">
<TextBox.Background>
<SolidColorBrush Color="White" Opacity="0.1" />
</TextBox.Background>
</TextBox>
<!--The button from which im trying to change the color-->
<Button
Background="Red"
Content="Connect"
DockPanel.Dock="Top"
Command="{Binding ConnectToServerCommand}"
Height="25">
<Button.Resources>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="5" />
</Style>
</Button.Resources>
</Button>
<ListView
Background="{x:Null}"
Foreground="White"
ItemsSource="{Binding Users}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Username}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</DockPanel>
<Grid Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="8*" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<ListView
Background="{x:Null}"
Foreground="White"
Height="Auto"
ItemsSource="{Binding Messages}" />
<StackPanel Grid.Row="1" Orientation="Horizontal">
<TextBox
Text="{Binding Message, UpdateSourceTrigger=PropertyChanged}"
VerticalContentAlignment="Center"
Width="520">
<TextBox.InputBindings>
<KeyBinding Command="{Binding SendMessageCommand}" Key="Enter" />
</TextBox.InputBindings>
<TextBox.Resources>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="6" />
</Style>
</TextBox.Resources>
</TextBox>
<Button
Command="{Binding SendMessageCommand}"
Content="Send"
Width="55">
<Button.Resources>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="5" />
</Style>
</Button.Resources>
</Button>
</StackPanel>
</Grid>
</Grid>
</Grid>
</Border>
</Window>

來自系結按鈕命令的視圖模型。
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows;
using ChatApplicatie.MVVM.CoreComponents;
using ChatApplicatie.MVVM.Model;
using ChatApplicatie.Net;
namespace ChatApplicatie.MVVM.ViewModel;
public class MainViewModel
{
public ObservableCollection<UserModel> Users { get; set; }
public ObservableCollection<string> Messages { get; set; }
public RelayCommand ConnectToServerCommand { get; set; }
public RelayCommand SendMessageCommand { get; set; }
public string Username { get; set; }
public string Message { get; set; }
private Server _server;
public MainViewModel()
{
Users = new ObservableCollection<UserModel>();
Messages = new ObservableCollection<string>();
_server = new Server();
_server.connectedEvent = UserConnected;
_server.msgReceivedEvent = MessageReceived;
_server.userDisconnectEvent = RemoveUser;
ConnectToServerCommand = new RelayCommand(o => _server.ConnectToServer(Username), o => !string.IsNullOrEmpty(Username));
SendMessageCommand = new RelayCommand(o => _server.SendMessageToServer(Message), o => !string.IsNullOrEmpty(Message));
}
private void RemoveUser()
{
var uid = _server.PacketReader.ReadMessage();
var user = Users.Where(x => x.UID == uid).FirstOrDefault();
Application.Current.Dispatcher.Invoke(() => Users.Remove(user));
}
private void MessageReceived()
{
var msg = _server.PacketReader.ReadMessage();
Application.Current.Dispatcher.Invoke(() => Messages.Add(msg));
}
private void UserConnected()
{
var user = new UserModel
{
Username = _server.PacketReader.ReadMessage(),
UID = _server.PacketReader.ReadMessage(),
};
if (!Users.Any(x => x.UID == user.UID))
{
Application.Current.Dispatcher.Invoke(() => Users.Add(user));
}
}
}
我不完全理解系結如何影響按鈕的其他屬性,并且我愿意尋求任何提示或幫助。
通過標準 xaml 更改背景屬性
使用 setter 通過 style 屬性更改背景屬性
洗掉命令系結會在按鈕中顯示顏色。
uj5u.com熱心網友回復:
您在按鈕屬性中設定的顏色用于按鈕的正常狀態。
在您的情況下,連接命令后,按鈕將被禁用(IsEnabled=false),因為您沒有將引數傳遞給命令。并且命令方法o => !string.IsNullOrEmpty(Username)需要命令的非空字串。
并且按鈕正常狀態以外的顏色由按鈕模板中的常量設定。因此,如果不更改按鈕模板就無法更改它們。
在您的情況下,要啟用按鈕,您需要設定命令引數系結。
<Button
Background="Red"
Content="Connect"
DockPanel.Dock="Top"
Command="{Binding ConnectToServerCommand}"
CommandParameter="{Binding UserName}"
Height="25">
<Button.Resources>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="5" />
</Style>
</Button.Resources>
</Button>
當您輸入UserName時,按鈕將打開并采用您設定的顏色。
RelayCommand實作代碼也很重要。在 WPF 中,命令必須自動訂閱CommandManager.RequerySuggested.
否則,更改引數時按鈕可能不會始終自動啟用。
我還擔心 MainViewModel 中可變屬性的實作。
如果一個屬性有一個設定器,那么它必須有一個通過 INotifyPropertyChanged.PropertyChanged 事件的內置屬性更改通知。
你沒有。因此,對 MainViewModel 屬性的更改可能不會總是自動更新系結到它們的屬性。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/524077.html
標籤:C#wpfxml虚拟机
