2

WPFで動作する次のxamlバインディングシナリオを取得しました。のリソースで定義UIElementsGridます。ToggleButtonこれらの静的リソースをのTagプロパティにバインドします。トグル ボタンをクリックして、Tagプロパティを のContentプロパティに割り当てますContentControl

<Grid>
    <Grid.Resources>
        <TextBlock x:Key="t1"
                   Grid.Row="1"
                   Text="Text1" />
        <TextBlock x:Key="t2"
                   Grid.Row="1"
                   Text="Text2" />
    </Grid.Resources>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <StackPanel Orientation="Horizontal"
                HorizontalAlignment="Center">
        <ToggleButton Tag="{StaticResource t1}"
                      Margin="10"
                      Click="ButtonBase_OnClick"
                      Content="T1" />
        <ToggleButton Tag="{StaticResource t2}"
                      Margin="10"
                      Click="ButtonBase_OnClick"
                      Content="T1" />
    </StackPanel>

    <ContentControl x:Name="cc"
                    Grid.Row="1" />
</Grid>

トグル ボタンをクリックすると、Tag 値がContentプロパティに割り当てられます。

private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
    cc.Content = ((FrameworkElement)sender).Tag;
}

これは万能の WPF では機能しますが、WinRT では不可能です。WinRT は、ArgumentException「値が期待される範囲内にありません。」というエラーを出します。理由がわかりません。

テスト目的で、イベント ハンドラーで直接割り当てを試みましたが、これは期待どおりに機能しました。

 cc.Content = new TextBlock { Text = "Text1" };

さらに奇妙にするために、WinRTでこれを試しました:

<ContentControl x:Name="cc"
                Content="{StaticResource t1}"
                Grid.Row="1" />

結果:デザイナーでは機能しますが、実行時に失敗します。それについてはさらに無知です。

まず、ArgumentException は何を伝えようとしているのでしょうか。次に、なぜ WPF で動作するのですか? ランタイムと VS デザイナーの不一致はどうですか?

4

1 に答える 1