私はWPFにかなり慣れていないので(現在3週間使用しています)、愚かなことを見逃しているか、自分が何をしているのか理解していない可能性があります!
アプリケーション全体または現在のコントロールのいずれかをカバーするモーダルタイプのポップアップウィンドウを作成しようとしています。このコントロールを半透明にして、ユーザーが背後のコンテンツを見ることができるが使用できないようにします。その後、続行する前に、モーダル ウィンドウにあるものをすべて完了することができます。
さまざまな場所でコードを繰り返す必要がないため、私の目標は、XAML で使用でき、毎回必要なコンテンツを追加するだけで済む汎用コントロールを用意することです。つまり、フェード、透明度、背景色のエクストラはすべて 1 か所で処理され、そのインスタンスに特定の機能を追加するだけで済みます。
これまでのところ、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」にブレークポイントを設定すると、新しいコンテンツが渡されていることがわかりますが、実行時に表示されません。
これがすべて間違っているのか、それが可能かどうか、または微調整が必要なだけかどうかはわかりません。これに関するすべてのアドバイスと、この種のシナリオへの対処は大歓迎です。