Avalonia 使用程式碼方式當 滑鼠移動到Button 上方 停用 Button背景顏色變化 保持 原本背景色和文字顏色
Avalonia 使用程式碼方式當 滑鼠移動到Button 上方 停用 Button背景顏色變化 保持 原本背景色和文字顏色
資料來源: ChatGPT(確定可用)
函數主體:
private void ConfigureButtonStyle(Button button)//Avalonia 使用程式碼方式當 滑鼠移動到Button 上方 停用 Button背景顏色變化 保持 原本背景色和文字顏色(by ChatGPT)
{
// 定義自訂模板來移除 PointerOver 狀態的影響
button.Template = new FuncControlTemplate<Button>((control, _) =>
{
return new Border
{
[!Border.BackgroundProperty] = control[!Button.BackgroundProperty], // 綁定按鈕的背景屬性
Child = new ContentPresenter
{
[!ContentPresenter.ContentProperty] = control[!Button.ContentProperty], // 綁定按鈕的內容
[!ContentPresenter.ForegroundProperty] = control[!Button.ForegroundProperty], // 綁定文字顏色
[!ContentPresenter.HorizontalAlignmentProperty] = control[!Button.HorizontalContentAlignmentProperty],
[!ContentPresenter.VerticalAlignmentProperty] = control[!Button.VerticalContentAlignmentProperty]
}
};
});
}
=============
實際應用xaml
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="VPOS_Avalonia.DateTimePicker"
Title="DateTimePicker" Loaded="Window_Loaded">
<Border BorderBrush="LightBlue" BorderThickness="5" CornerRadius="10" Margin="-3">
<Grid x:Name="FullGrid" RowDefinitions="1.5*,6*,2.5*" ColumnDefinitions="*" >
<Border BorderBrush="Blue" BorderThickness="1" CornerRadius="10" Margin="0" Grid.Row="0" Grid.Column="0">
<Grid x:Name="HeadGrid" RowDefinitions="*" ColumnDefinitions="*">
<TextBlock x:Name="TitleLable" Text="日期/時間 選擇" Foreground="White" FontSize="22" Grid.Row="0" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Center" />
<Button x:Name="CloseBtn" Content="X" Grid.Row="0" Grid.Column="0" FontSize="20" Background="Transparent" Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0,0,10,0" Click="CloseBtn_Clicked"/>
</Grid><!--HeadGrid End-->
</Border>
<Border x:Name="BodyBorder" BorderBrush="White" BorderThickness="1" CornerRadius="10" Margin="5" Grid.Row="1" Grid.Column="0">
<Grid x:Name="BodyGrid" RowDefinitions="*,*" ColumnDefinitions="1*,1.5*,30,1.5*,*" Margin="20,0,10,0">
<TextBlock Text="日期:" VerticalAlignment="Center" Foreground="White" FontSize="20" Grid.Row="0" Grid.Column="0" />
<DatePicker x:Name="DatePicker01" Width="300" VerticalAlignment="Center" HorizontalAlignment="Left" Background="White" FontSize="22" Grid.Row="0" Grid.Column="1" Margin="5" />
<TextBlock Text="時間:" VerticalAlignment="Center" Foreground="White" FontSize="20" Grid.Row="1" Grid.Column="0" />
<TimePicker Name="TimePicker" ClockIdentifier="24HourClock" Width="300" VerticalAlignment="Center" HorizontalAlignment="Left" Background="White" FontSize="22" Grid.Row="1" Grid.Column="1" Margin="5" />
</Grid>
</Border>
<Grid x:Name="FooterGrid" RowDefinitions="*" ColumnDefinitions="0.2*,*,*,0.2*" Grid.Row="2" Grid.Column="0" >
<Button x:Name="FooterBtn01" Content="確定" Foreground="White" Background="#cc6600" Grid.Row="0" Grid.Column="1" Click="FooterBtn01_Clicked" FontSize="20" Height="50" HorizontalAlignment="Stretch" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Margin="30,0"/>
<Button x:Name="FooterBtn02" Content="取消" Foreground="White" Background="#cc6600" Grid.Row="0" Grid.Column="2" Click="CloseBtn_Clicked" FontSize="20" Height="50" HorizontalAlignment="Stretch" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Margin="30,0"/>
</Grid>
</Grid><!--FullGrid End-->
</Border>
</Window>
實際應用code
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Presenters;
using Avalonia.Controls.Templates;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using Avalonia.Media;
using System;
using VPOS_Avalonia.ViewModels;
namespace VPOS_Avalonia;
public partial class DateTimePicker : Window
{
public static bool m_blnRun = false;
public static string m_StrResult = "";
public DateTimePicker()
{
//隱藏工具列
this.ExtendClientAreaToDecorationsHint = true;
this.ExtendClientAreaTitleBarHeightHint = -1;
this.SystemDecorations = SystemDecorations.None;
//---隱藏工具列
WindowStartupLocation = WindowStartupLocation.CenterScreen;//視窗置中
this.Background = new SolidColorBrush(Color.Parse("#ff194a6e"));//設定背景色
//this.WindowState = WindowState.Maximized;//視窗最大化非滿版
InitializeComponent();
this.Width = 500;
this.Height = 300;
FullGrid.IsVisible = false;
}
public void Window_Loaded(object sender, RoutedEventArgs e)
{
FullGrid.IsVisible = true;
ConfigureButtonStyle(CloseBtn);
ConfigureButtonStyle(FooterBtn01);
ConfigureButtonStyle(FooterBtn02);
DatePicker01.SelectedDate = DateTime.Now.Date; // 設置初始日期
TimePicker.SelectedTime = DateTime.Now.TimeOfDay; // 設置初始時間
}
private void ConfigureButtonStyle(Button button)//Avalonia 使用程式碼方式當 滑鼠移動到Button 上方 停用 Button背景顏色變化 保持 原本背景色和文字顏色(by ChatGPT)
{
// 定義自訂模板來移除 PointerOver 狀態的影響
button.Template = new FuncControlTemplate<Button>((control, _) =>
{
return new Border
{
[!Border.BackgroundProperty] = control[!Button.BackgroundProperty], // 綁定按鈕的背景屬性
Child = new ContentPresenter
{
[!ContentPresenter.ContentProperty] = control[!Button.ContentProperty], // 綁定按鈕的內容
[!ContentPresenter.ForegroundProperty] = control[!Button.ForegroundProperty], // 綁定文字顏色
[!ContentPresenter.HorizontalAlignmentProperty] = control[!Button.HorizontalContentAlignmentProperty],
[!ContentPresenter.VerticalAlignmentProperty] = control[!Button.VerticalContentAlignmentProperty]
}
};
});
}
private void CloseBtn_Clicked(object sender, RoutedEventArgs e)
{
m_blnRun = false;
m_StrResult = "";
Close();
}
private void FooterBtn01_Clicked(object sender, RoutedEventArgs e)
{
if((DatePicker01.SelectedDate!=null) &&(TimePicker.SelectedTime!=null))
{
m_blnRun = true;
m_StrResult = DatePicker01.SelectedDate.Value.Date.ToString("yyyy-MM-dd") + $" {TimePicker.SelectedTime.Value.Hours.ToString().PadLeft(2,'0')}:{TimePicker.SelectedTime.Value.Minutes.ToString().PadLeft(2, '0')}:00";
this.Close();
}
}
}
3 thoughts on “Avalonia 使用程式碼方式當 滑鼠移動到Button 上方 停用 Button背景顏色變化 保持 原本背景色和文字顏色”
我要 正式跟 CHATGPT 道歉
因為 我今天問 Avalonia 使用程式碼方式當 滑鼠移動到Button 上方 停用 Button背景顏色變化 保持 原本背景色和文字顏色 10個AI只有他的答案能動
我昨天還因為這新聞下面這新聞笑他 真是大不敬
https://www.msn.com/zh-tw/news/world/chatgpt%E4%BB%8A%E6%99%A8%E5%82%B3%E5%85%A8%E7%90%83%E7%95%B6%E6%A9%9F-%E7%84%A1%E6%B3%95%E4%BD%BF%E7%94%A8/ar-AA1vHrgQ?ocid=msedgdhp&pc=U531&cvid=e8a8b7b2beef4005b6bfb4f29d01fd41&ei=29
A:當一個稱職的白嫖 & 躺平耍廢 仔
今天有錯就要認 做不出來就要講 大不了就是被資遣
B:氣魄好!台語叫「叫小(淆)好」,…很多長官欣賞這種的。
A: 當長官都不知道在想什麼?
ChatGPT程式原理說明:
▲自訂模板 (FuncControlTemplate):
01.使用 FuncControlTemplate 自訂按鈕外觀,透過 Border 和 ContentPresenter 顯示內容。
02.按鈕的所有屬性(例如背景、文字顏色、內容)都使用綁定 ([!]) 動態連接到按鈕的屬性。
禁用預設樣式:
▲沒有引入任何狀態變化(如 :pointerover 或 :pressed),因此滑鼠懸停時按鈕的外觀完全保持靜止。
▲保留按鈕功能:
雖然外觀變化被禁用,但按鈕的點擊和其他互動行為(例如 Click 事件)仍正常運作。