1

簡単なSilverlightアプリケーションを作成しました。デザイン時にスタイルが正しく表示されますが、アプリケーションを実行しようとすると、app.xamlファイルにマージされたリソースディクショナリファイルのスタイルは、実行時にどのコントロールにも適用されません。

実際には、UserControlスタイルだけが当てはまらないようです。しかし、残りは機能しています(Buttonページのように)。この問題の原因は何で、どうすれば修正できますか?

私のコードは次のようなものです:

Styles.xaml:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style TargetType="UserControl">
        <Setter Property="FlowDirection" Value="RightToLeft" />
        <Setter Property="FontFamily" Value="Tahoma" />
        <Setter Property="Background" Value="Aqua" />
    </Style>
    <Style TargetType="Button" >
        <Setter Property="Background" Value="Aqua" />
    </Style>
</ResourceDictionary>

App.xaml:

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             x:Class="Silverlight.Test._01.App"
             >
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Styles.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

MainPage.xaml:

<UserControl x:Class="Silverlight.Test._01.MainPage"
    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"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">

    <Grid x:Name="LayoutRoot" Background="White">
        <Button Content="This is a test" Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="button1" VerticalAlignment="Top" Width="220"   />
        <sdk:Label   Height="28" HorizontalAlignment="Left" Margin="12,6,0,0" Name="label1" VerticalAlignment="Top" Width="351" Content="Test label" />
    </Grid>
</UserControl>
4

1 に答える 1

4

これが機能しない理由の少なくとも1つは、実際にのインスタンスを作成することがないためですUserControl。実際にのインスタンスを作成しますSilverlight.Test._01.MainPage

さらに、とは異なりButton、コントロールUserControlDefaultStyleKeyプロパティを実際にコードビハインドでUserControl値を設定しようとすると、例外が発生します。DefaultStyleKey

これに対する一般的な回避策はありません。最も近い方法は、デフォルトのスタイルを標準のキー付きリソースに変更することです。-

<Style x:Key="UserControlDefaultStyle" TargetType="UserControl">
    <Setter Property="FlowDirection" Value="RightToLeft" />
    <Setter Property="FontFamily" Value="Tahoma" />
    <Setter Property="Background" Value="Aqua" />
</Style> 

次に、usercontrolxamlを次のように変更します。-

<UserControl x:Class="Silverlight.Test._01.MainPage"
    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"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
    Style="{StaticResource UserControlDefaultStyle}"    
>

    <Grid x:Name="LayoutRoot" Background="{Binding Parent.Background, ElementName=LayoutRoot}">
        <Button Content="This is a test" Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="button1" VerticalAlignment="Top" Width="220"   />
        <sdk:Label   Height="28" HorizontalAlignment="Left" Margin="12,6,0,0" Name="label1" VerticalAlignment="Top" Width="351" Content="Test label" />
    </Grid>
</UserControl>

Style作成するそれぞれに追加の属性を追加する必要があるため、これは一般的な解決策ではないことに注意してくださいUserControl

LayoutRootBackgroundプロパティのバインドにも注意してください。UserControl.Backgroundプロパティは実際には何もしません。この値を子コントロールに渡すと、効果があります。

于 2011-01-02T23:22:05.843 に答える