我正在尝试通过弹出的设置窗口实时更改主应用程序窗口的不透明度
。这样做的正确方法是什么?
到目前为止,我已尝试使用 Slider
将值输出到设置文件。当设置弹出窗口关闭时,主窗口会根据设置文件刷新其不透明度属性。此方法有效,但我希望能够更改不透明度并实时查看结果。
我尝试的第二种方法是使用Style
并将其应用到MainWindow。然后,在 slider 移动时,样式将被 slider 中的值覆盖。这是实时进行的。但无论出于何种原因,即使没有应用任何样式,设置弹出窗口的不透明度也会受到影响。
这是一个名为 OpacityTest
的示例项目,其中包含一个主窗口、一个用于打开设置弹出窗口的按钮和用于控制程序不透明度的 slider 。
应用程序.xaml:
<Application x:Class="OpacityTest.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style TargetType="Window" x:Key="wrapper">
<Setter Property="OverridesDefaultStyle" Value="false"/>
<Setter x:Name="opacitySetter" Property="Opacity" Value="1"/>
</Style>
</Application.Resources>
</Application>
MainWindow.xaml:
<Window x:Class="OpacityTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Style="{DynamicResource wrapper}" Background="#FFCDCDCD" AllowsTransparency="True" WindowStyle="None">
<Grid>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<Button Content="Settings" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Click="Button_Click"/>
</StackPanel>
</Grid>
</Window>
标有“设置”、“Settings.xaml”的新窗口:
<Window x:Class="OpacityTest.Settings"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Settings" Height="300" Width="300">
<Grid>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<Slider x:Name="ChangeTransparency" MinWidth="138" MinHeight="22" VerticalAlignment="Center" Padding="0" Orientation="Horizontal" HorizontalAlignment="Center" Value="1" Minimum=".05" Maximum="1" LargeChange=".01" SmallChange=".01" TickFrequency="100" IsSnapToTickEnabled="False" MouseMove="ChangeTransparency_MouseMove"/>
</StackPanel>
</Grid>
</Window>
MainWindow.xaml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace OpacityTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Settings settings = new Settings();
settings.ShowDialog();
}
}
}
设置.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace OpacityTest
{
/// <summary>
/// Interaction logic for Settings.xaml
/// </summary>
public partial class Settings : Window
{
public Settings()
{
InitializeComponent();
}
private void ChangeTransparency_MouseMove(object sender, MouseEventArgs e)
{
Style style = new Style { TargetType = typeof(Window) };
style.Setters.Add(new Setter(OpacityProperty, Opacity = ChangeTransparency.Value));
Application.Current.Resources["wrapper"] = style;
}
}
}
最佳答案
看起来您的 Style
的行为就像在 XAML 文件中声明的那样,影响了所有 Window
,而不是只是 wrapper
样式,例如
<Style TargetType="Window"> ... </Style>
而不是:
<Style TargetType="Window" x:Key="wrapper"> ... </Style>
我不确定如何按照您想要的方式声明它,但完成您想要做的事情的一种更简单的方法是将其放入 MainWindow.xaml.cs 中:
private void Button_Click(object sender, RoutedEventArgs e)
{
Settings settings = new Settings();
this.SetBinding(OpacityProperty,
new Binding("Value") { Source = settings.ChangeTransparency });
settings.ShowDialog();
}
这种方式不需要 ChangeTransparency
上的处理程序。我可能考虑修改 Style
的唯一原因是,如果您可以同时拥有多个 MainWindow
,并且想要一个 Settings
窗口(从任何一个)来同时控制它们。
顺便说一句,如果您确实发现需要附加到 ChangeTransparency
,则应该将处理程序附加到其 ValueChanged
事件而不是 MouseMove
(虽然 MouseMove
通常会起作用,但实际上并不相同;例如,对于键盘输入,只有 ValueChanged
会触发)。
如果Settings
可以从其他窗口使用和/或具有更多属性,您可能希望更改Settings
以采用Window
(或其他类型)在其构造函数中,并在那里设置绑定(bind),以保持代码/逻辑集中且干净。
关于c# - 如何通过设置窗口实时控制程序的不透明度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17034756/