我对 WPF 很陌生,(现在使用它 3 周),所以我可能会错过一些愚蠢的东西或不明白我在做什么!
我正在尝试创建一个模式类型的弹出窗口,它将覆盖整个应用程序或它所在的当前控件。我希望这个控件是半透明的,这样用户仍然可以看到后面的内容,但不能使用它。然后,他们将能够在继续之前完成模式窗口中的内容。
我不想在不同的地方重复代码,所以我的目标是拥有一个可以在我的 XAML 中使用的通用控件,并且每次只需要添加我需要的内容。即褪色、透明度、额外的背景颜色都在一个地方处理,我只需要为它的那个实例添加特定的功能。
到目前为止,我已经创建了一个名为 jonblind 的用户控件:
<UserControl x:Class="ShapInteractiveClient.View.SampleTests.jonblind"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid x:Name="blindGrid" Grid.RowSpan="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Opacity="0.82">
<Grid.Background>
<LinearGradientBrush EndPoint="0,1" StartPoint="0,0" MappingMode="RelativeToBoundingBox">
<GradientStop Color="#FFA8CBFE" Offset="1"/>
<GradientStop Color="Red"/>
<GradientStop Color="#FFE1EDFE" Offset="0.147"/>
</LinearGradientBrush>
</Grid.Background>
<ContentControl x:Name="contentTemplateArea" />
</Grid>
</UserControl>
我有一个控制代码如下:
public partial class jonblind : UserControl
{
public jonblind()
{
InitializeComponent();
SetVisibility(this);
}
[Category("jonblind")]
public bool IsContentVisible
{
get { return (bool)GetValue(IsContentVisibleProperty); }
set { SetValue(IsContentVisibleProperty, value); }
}
public static readonly DependencyProperty IsContentVisibleProperty = DependencyProperty.Register("IsContentVisible", typeof(bool), typeof(jonblind),
new FrameworkPropertyMetadata(new PropertyChangedCallback(OnIsOverlayContentVisibleChanged)));
private static void OnIsOverlayContentVisibleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
jonblind blind = d as jonblind;
if(blind != null)
SetVisibility(blind);
}
private static void SetVisibility(jonblind blind)
{
blind.blindGrid.Visibility = blind.IsContentVisible ? Visibility.Visible : Visibility.Hidden;
}
[Category("jonblind")]
public ContentControl ContentAreaControl
{
get { return (ContentControl)GetValue(ContentAreaControlProperty); }
set { SetValue(ContentAreaControlProperty, value); }
}
public static readonly DependencyProperty ContentAreaControlProperty = DependencyProperty.Register("ContentAreaControl", typeof(ContentControl), typeof(jonblind),
new FrameworkPropertyMetadata(new PropertyChangedCallback(OnContentAreaControlChanged)));
private static void OnContentAreaControlChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
jonblind blind = d as jonblind;
if (blind != null && e.NewValue != null && e.NewValue is ContentControl)
{
blind.contentTemplateArea = e.NewValue as ContentControl;
}
}
}
我可以将它添加到另一个用户控件,如下所示:
<UserControl.Resources>
<ContentControl x:Key="testcontrol">
<StackPanel>
<TextBox VerticalAlignment="Center" HorizontalAlignment="Center" Text="Loading!!!" />
<Button Content="hide me!" Command="{Binding Path=alternateblind}" />
</StackPanel>
</ContentControl>
</UserControl.Resources>
<SampleTests:jonblind IsContentVisible="{Binding Path=ShowBlind}" ContentAreaControl="{StaticResource testcontrol}" />
如果我在“OnContentAreaControlChanged”上放置一个断点,我可以看到传入的新内容,但它永远不会在运行时显示它。
我不知道我是否将这一切都弄错了,是否有可能,或者是否只需要 tweeking。对此和处理这种情况的任何和所有建议将不胜感激。