10

内でWindowStartupLocationプロパティを設定しようとすると、次のようになります。SetterResourceDictionaryXamlParseException

'プロパティの設定'System.Windows.Setter.Property'が例外をスローしました。行番号「x」と行位置「y」。

内部の例外はArgumentNullException

値をnullにすることはできません。パラメータ名:プロパティ。

リソース辞書内の私のスタイルは次のとおりです。

<Style TargetType="Window" x:Key="WindowStyle">
    <Setter Property="SizeToContent" Value="WidthAndHeight" />
    <Setter Property="ResizeMode" Value="CanMinimize" />
    <Setter Property="WindowStartupLocation" Value="CenterOwner" />
</Style>

ResourceDictionaryを削除すると、スタイルを参照するウィンドウでWindowStartupLocation他の2つのプロパティ(SizeToContentおよび)が期待どおりに設定されるため、問題はの使用ではありません。ResizeMode

<Window x:Class="WpfApplication1.MyWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Style="{DynamicResource WindowStyle}">
    <Window.Resources>
        <ResourceDictionary Source="MyResourceDictionary.xaml" />
    </Window.Resources>
</Window>

誰かがこれに遭遇しましたか?WPFのバグ/制限ですか?

PSこの質問はリソースディクショナリのウィンドウ起動場所に似ていることは知っていますが、他の質問では十分な情報が提供されておらず、その後未解決のままでした。

4

2 に答える 2

14

問題は、WindowStartupLocation が DependencyProperty ではないため、スタイル セッターで設定できないことです。ILSpy を見ると、セッターが呼び出します

CheckValidProperty(DependencyProperty property)

NullArgumentException をスローします。

WindowStartupLocation は単なる CLR プロパティであるため、この方法で設定することはできません。

Edit:コメントに返信するため。引き続き使用できますResourceDictionary

<Application.Resources>
    <ResourceDictionary>
        <Style x:Key="WindowStyle" TargetType="Window">
            <Setter Property="SizeToContent" Value="WidthAndHeight" />
            <Setter Property="ResizeMode" Value="CanMinimize" />
        </Style>
        <WindowStartupLocation x:Key="WSL">CenterOwner</WindowStartupLocation>
    </ResourceDictionary>
</Application.Resources>

<Window x:Class="WpfApplication7.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"            
        WindowStartupLocation="{StaticResource WSL}"
        Style="{StaticResource WindowStyle}" />
于 2012-05-15T08:27:42.857 に答える