8

次のユーザー コントロールを作成しました。xaml ウィンドウに追加すると、「"ucAppItem" のインスタンスを作成できません。ユーザー コントロールをツールバーからウィンドウにドラッグしました。

ユーザー コントロールの XAML は次のとおりです。

<UserControl x:Class="Demos.ucAppItem"
             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" Width="852" Height="215">
    <Grid>

        <Label Name="lblTitle"  Content="Title" HorizontalAlignment="Left" Margin="233,10,0,0" VerticalAlignment="Top" FontSize="22" FontFamily="Arial"/>


        <Image Width="40" Height="40" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0,0,80,0">
            <Image.Style>
                <Style TargetType="{x:Type Image}">
                    <Setter Property="Source" Value="pack://siteoforigin:,,,/arrow2.png"/>
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Source" Value="pack://siteoforigin:,,,/arrow1.png"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Image.Style>
        </Image>
        <Label x:Name="lblRun" Content="Run" HorizontalAlignment="Right" Margin="0,88,35,0" VerticalAlignment="Top" Foreground="#FF2EAADC" FontSize="20">
            <Label.Style>
                <Style TargetType="{x:Type Label}">
                    <Setter Property="Foreground" Value="#FF2EAADC"/>
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Foreground" Value="#006d9e"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Label.Style>
        </Label>
    </Grid>
</UserControl>

ウィンドウの XAML は次のとおりです。

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Demos" x:Class="Demos.Window1"
        Title="Window1" Height="487" Width="854">
    <Grid>

        <local:ucAppItem/>

    </Grid>
</Window>

事前にご協力いただきありがとうございます。

4

2 に答える 2

4

@Anatoily Nikolaev - ご協力ありがとうございます。ラベル上のあなたのポインターは、私が抱えていた問題を修正し、あなたは画像について正しかった. あなたの回答を回答としてマークします。問題となったのはソースでした。

私のラベルは次のように定義されています。

   <Label x:Name="lblRun" Content="Run" HorizontalAlignment="Right" Margin="0,88,35,0" VerticalAlignment="Top" FontSize="20">
        <Label.Style>
            <Style TargetType="{x:Type Label}">
                <Setter Property="Foreground" Value="#FF2EAADC"/>
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Foreground" Value="#006d9e"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Label.Style>
    </Label>

私のイメージは次のように定義されています。

       <Image>
            <Image.Style>
                <Style TargetType="{x:Type Image}">
                    <Setter Property="Source" Value="{StaticResource arrow2}"/>
                    <Setter Property="Height" Value="40"/>
                    <Setter Property="Width" Value="40"/>
                    <Setter Property="VerticalAlignment" Value="Center"/>
                    <Setter Property="Margin" Value="0,0,80,0"/>
                    <Setter Property="HorizontalAlignment" Value="Right"/>
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Source" Value="{StaticResource arrow1}"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Image.Style>
        </Image>

そして、次のように設定されたリソース (App.xaml ファイルに設定) があります。

<Application x:Class="demos.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <BitmapImage x:Key="arrow1" UriSource="arrow1.png" />
        <BitmapImage x:Key="arrow2" UriSource="arrow2.png" />
    </Application.Resources>
</Application>
于 2013-08-31T21:32:05.380 に答える
3

まず、pack://siteoforigin:,,,/arrow2.png実際のファイルを記述URIして、ファイルがリソースとしてプロジェクトに存在することを確認する必要はありません ( MSDN):

pack://application:,,,/arrow1.png

第 2 に、ラベルのトリガー スタイルは機能しlblRunません。これは、このForeground値をローカルに設定するためです。WPF には値の優先順位のリスト ( MSDN) があり、ローカル値はトリガー スタイルよりも優先度が高くなります。

<Label x:Name="lblRun" Foreground="#FF2EAADC" FontSize="20" ... />

Foregroundローカル値を削除して、 Stylesetterを使用してみてください。

<Label x:Name="lblRun" Content="Run" HorizontalAlignment="Right" Margin="0,88,35,0" VerticalAlignment="Top" FontSize="20">
    <Label.Style>
        <Style TargetType="{x:Type Label}">
            <Setter Property="Foreground" Value="#FF2EAADC"/>

            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Foreground" Value="#006d9e"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Label.Style>
</Label>
于 2013-08-31T20:48:10.870 に答える