我如何在模糊 WPF 中的表单的其余部分时保持一个控件正常
How do i keep one control normal while Blurring the rest of the form in WPF
在下面的代码中,我可以模糊掉所有的控件。但是我想阻止其中一个按钮的效果。
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
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"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="MainGrid">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button x:Name="button1" Content="Button 1" Margin="5"/>
<Button x:Name="button2" Content="Button 2" Margin="5" Grid.Column="1"/>
<Button x:Name="button3" Content="Button 3" Margin="5" Grid.Column="2"/>
<Button x:Name="button4" Content="Button 4" Margin="5" Grid.Row="1"/>
<Button x:Name="button5" Content="Button 5" Margin="5" Grid.Column="1" Grid.Row="1" Click="button5_Click"/>
<Button x:Name="button6" Content="Button 6" Margin="5" Grid.Row="1" Grid.Column="2"/>
<Button x:Name="button7" Content="Button 7" Margin="5" Grid.Row="2"/>
<Button x:Name="button8" Content="Button 8" Margin="5" Grid.Column="1" Grid.Row="2"/>
<Button x:Name="button9" Content="Button 9" Margin="5" Grid.Column="2" Grid.Row="2"/>
</Grid>
模糊处理背后的代码:
private void button5_Click(object sender, RoutedEventArgs e)
{
BlurEffect blurEffect = new BlurEffect();
blurEffect.Radius = 3;
MainGrid.Effect = blurEffect;
}
我已经尝试添加下面的代码来反转我想要保持正常的按钮的过程,但它没有用。
BlurEffect blurEffect = new BlurEffect();
blurEffect.Radius = 0;
button5.Effect = blurEffect;
有没有一种方法可以在不单独模糊表单上的所有控件的情况下完成此操作?
谢谢
PS。为了更清楚起见,这只是一个简化的示例
好的,您应该使用 MVVM 而不是按钮单击处理程序。您正在对整个控件应用模糊,而您应该在控件的某些部分上进行模糊处理。但是让我们看看。
首先你应该像这样定义模糊的样式。
<Grid.Resources>
<Style x:Key="BlurredButton" TargetType="{x:Type Button}">
<Setter Property="Effect">
<Setter.Value>
<BlurEffect Radius="3"/>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
然后在您的按钮单击处理程序中,您应该遍历网格的按钮并有一些逻辑来检查应该重新设置哪些按钮的样式。在这里,我没有设置发件人的样式。我们将新样式应用于所有按钮。
private void button5_Click(object sender, RoutedEventArgs e)
{
foreach (var child in this.MainGrid.Children.OfType<Button>())
{
if (!object.Equals(sender, child))
{
child.Style = this.MainGrid.Resources["BlurredButton"] as Style;
}
}
}
或者第二个选项就是像这样让 2 个网格彼此重叠
<Grid>
<Grid x:Name="MainGrid">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button x:Name="button1" Content="Button 1" Margin="5"/>
<Button x:Name="button2" Content="Button 2" Margin="5" Grid.Column="1"/>
<Button x:Name="button3" Content="Button 3" Margin="5" Grid.Column="2"/>
<Button x:Name="button4" Content="Button 4" Margin="5" Grid.Row="1"/>
<Button x:Name="button6" Content="Button 6" Margin="5" Grid.Row="1" Grid.Column="2"/>
<Button x:Name="button7" Content="Button 7" Margin="5" Grid.Row="2"/>
<Button x:Name="button8" Content="Button 8" Margin="5" Grid.Column="1" Grid.Row="2"/>
<Button x:Name="button9" Content="Button 9" Margin="5" Grid.Column="2" Grid.Row="2"/>
</Grid>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button x:Name="button5" Content="Button 5" Margin="5" Grid.Column="1" Grid.Row="1" Click="button5_Click"/>
</Grid>
</Grid>
在下面的代码中,我可以模糊掉所有的控件。但是我想阻止其中一个按钮的效果。
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
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"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="MainGrid">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button x:Name="button1" Content="Button 1" Margin="5"/>
<Button x:Name="button2" Content="Button 2" Margin="5" Grid.Column="1"/>
<Button x:Name="button3" Content="Button 3" Margin="5" Grid.Column="2"/>
<Button x:Name="button4" Content="Button 4" Margin="5" Grid.Row="1"/>
<Button x:Name="button5" Content="Button 5" Margin="5" Grid.Column="1" Grid.Row="1" Click="button5_Click"/>
<Button x:Name="button6" Content="Button 6" Margin="5" Grid.Row="1" Grid.Column="2"/>
<Button x:Name="button7" Content="Button 7" Margin="5" Grid.Row="2"/>
<Button x:Name="button8" Content="Button 8" Margin="5" Grid.Column="1" Grid.Row="2"/>
<Button x:Name="button9" Content="Button 9" Margin="5" Grid.Column="2" Grid.Row="2"/>
</Grid>
模糊处理背后的代码:
private void button5_Click(object sender, RoutedEventArgs e)
{
BlurEffect blurEffect = new BlurEffect();
blurEffect.Radius = 3;
MainGrid.Effect = blurEffect;
}
我已经尝试添加下面的代码来反转我想要保持正常的按钮的过程,但它没有用。
BlurEffect blurEffect = new BlurEffect();
blurEffect.Radius = 0;
button5.Effect = blurEffect;
有没有一种方法可以在不单独模糊表单上的所有控件的情况下完成此操作?
谢谢
PS。为了更清楚起见,这只是一个简化的示例
好的,您应该使用 MVVM 而不是按钮单击处理程序。您正在对整个控件应用模糊,而您应该在控件的某些部分上进行模糊处理。但是让我们看看。
首先你应该像这样定义模糊的样式。
<Grid.Resources>
<Style x:Key="BlurredButton" TargetType="{x:Type Button}">
<Setter Property="Effect">
<Setter.Value>
<BlurEffect Radius="3"/>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
然后在您的按钮单击处理程序中,您应该遍历网格的按钮并有一些逻辑来检查应该重新设置哪些按钮的样式。在这里,我没有设置发件人的样式。我们将新样式应用于所有按钮。
private void button5_Click(object sender, RoutedEventArgs e)
{
foreach (var child in this.MainGrid.Children.OfType<Button>())
{
if (!object.Equals(sender, child))
{
child.Style = this.MainGrid.Resources["BlurredButton"] as Style;
}
}
}
或者第二个选项就是像这样让 2 个网格彼此重叠
<Grid>
<Grid x:Name="MainGrid">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button x:Name="button1" Content="Button 1" Margin="5"/>
<Button x:Name="button2" Content="Button 2" Margin="5" Grid.Column="1"/>
<Button x:Name="button3" Content="Button 3" Margin="5" Grid.Column="2"/>
<Button x:Name="button4" Content="Button 4" Margin="5" Grid.Row="1"/>
<Button x:Name="button6" Content="Button 6" Margin="5" Grid.Row="1" Grid.Column="2"/>
<Button x:Name="button7" Content="Button 7" Margin="5" Grid.Row="2"/>
<Button x:Name="button8" Content="Button 8" Margin="5" Grid.Column="1" Grid.Row="2"/>
<Button x:Name="button9" Content="Button 9" Margin="5" Grid.Column="2" Grid.Row="2"/>
</Grid>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button x:Name="button5" Content="Button 5" Margin="5" Grid.Column="1" Grid.Row="1" Click="button5_Click"/>
</Grid>
</Grid>