1

イメージとテキストを含むボタンを作成しようとしています。ただし、イメージは実行時にコード ビハインドによって変更される可能性があります。私の画像はpngで、全員が私のResourceDictionaryに次のように含まれています:

<BitmapImage x:Key="iconLogIn" UriSource="/Images/Icons/lock.png" />

したがって、私は Template や ContentTemplate を使用せずに、このスタイルから始めました。

<Style x:Key="MyTestButton" TargetType="{x:Type Button}">
    <Setter Property="Height" Value="80" />
    <Setter Property="Width" Value="100" />
</Style>

そして私のXAMLは次のとおりです。

<Button x:Name="cmdOption1" Style="{StaticResource MyTestButton}" Margin="8" Click="cmdOption1_Click">
    <Image Source="{DynamicResource iconLogIn}" />
</Button>

次に、画像を変更するための私のコードは次のとおりです。

cmdOption1.Content = new Image() { Source = ((BitmapImage)FindResource("iconLogOut")) };

これまでのところ、これは機能します。

しかし、画像を保持するだけで、画像の下にテキストを配置したい。

だから私はこの投稿を読みました.HighCoreの答え、オプション#2は私の要件を満たすかもしれません. しかし、ここで新たな問題が発生します。

まず、これはシンプルなテンプレートを使用した新しいスタイルです

<Style x:Key="MyTestButton" TargetType="{x:Type Button}">
    <Setter Property="Background" Value="#EEEEEE" />
    <Setter Property="Foreground" Value="DarkSlateGray" />
    <Setter Property="Height" Value="80" />
    <Setter Property="Width" Value="100" />
    <Setter Property="HorizontalContentAlignment" Value="Center"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="Padding" Value="2"/>
    <Setter Property="FontSize" Value="12" />
    <Setter Property="FontWeight" Value="SemiBold" />
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="BorderBrush" Value="DarkGray" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border>
                    <StackPanel>
                        <Image Source="{Binding Tag, RelativeSource={RelativeSource TemplatedParent}}" Height="50" Width="50"/>
                        <ContentPresenter Grid.Row="0" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" RecognizesAccessKey="True" />
                    </StackPanel>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter Property="Foreground" Value="DarkOrange" />
                        <Setter Property="OpacityMask" Value="#AA888888"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="Gray" />
                        <Setter Property="BorderBrush" Value="DarkGray" />
                        <Setter Property="Background" Value="White" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
</Style>

私の新しい XAML は次のとおりです。

<Button x:Name="cmdOption1" Tag="{StaticResource iconLogIn}" Content="LOG IN" Style="{StaticResource MyTestButton}" Margin="8" Click="cmdOption1_Click" />

※Tag="{DynamicResource iconLogIn}" とも

そして、画像とテキストを変更する私のコードビハインド:

cmdOption1.Tag = new Image() { Source = ((BitmapImage)FindResource("iconLogOut")) };
cmdOption1.Content = "LOG OUT";

これにより、コンテンツテキストが「LOG IN」から「LOG OUT」に変わります。しかし、画像は表示されなくなり、画像の代わりに何も表示されず、エラーや例外もスローされません。

解決策と何が起こっているのか知りたいのですが、なぜ画像が変化せずに消えたのですか?

前もって感謝します。

4

3 に答える 3

0

新しい Image コントロールを作成し、それを Button のTagプロパティに割り当てます。したがって、ControlTemplate の Image コントロールは、Source別の Image コントロールに設定されたプロパティを取得します。それはうまくいきません。Resources から BitmapImage を に割り当てるだけTagです。

それ以外の

cmdOption1.Tag = new Image() { Source = ((BitmapImage)FindResource("iconLogOut")) };

あなたは単にこれを書くべきです:

cmdOption1.Tag = (BitmapImage)FindResource("iconLogOut");
于 2013-08-07T08:09:54.360 に答える
0

XAML は正常に動作します。この方法でを設定するために必要なコード内の唯一のものImage:

private void cmdOption1_Click(object sender, RoutedEventArgs e)
{
    BitmapImage MyBitmapImage = ((BitmapImage)FindResource("iconLogOut"));
    cmdOption1.Tag = MyBitmapImage;

    cmdOption1.Content = "LOG OUT";
}

Tag代わりに (比較方法のみ)、関数経由で使用せずにこのトリックを使用できますFindChild<>。テンプレートのあなたの部分:

<ControlTemplate TargetType="{x:Type Button}">
    <Border>
        <StackPanel>
            <Image x:Name="MyContentImage" Source="{StaticResource iconLogIn}" Height="50" Width="50" />
            <ContentPresenter Grid.Row="0" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" RecognizesAccessKey="True" />
        </StackPanel>
    </Border>

    ...

コードビハインド:

private void cmdOption1_Click(object sender, RoutedEventArgs e)
{
    // Find the Image in template
    Image MyContentImage = FindChild<Image>(cmdOption1, "MyContentImage");
    MyContentImage.Source = ((BitmapImage)FindResource("iconLogOut")); 

    cmdOption1.Content = "LOG OUT";
}

のリストFindChild<>:

    public static T FindChild<T>(DependencyObject parent, string childName) where T : DependencyObject
    {
        if (parent == null)
        {
            return null;
        }

        T foundChild = null;

        int childrenCount = VisualTreeHelper.GetChildrenCount(parent);

        for (int i = 0; i < childrenCount; i++)
        {
            var child = VisualTreeHelper.GetChild(parent, i);
            T childType = child as T;

            if (childType == null)
            {
                foundChild = FindChild<T>(child, childName);

                if (foundChild != null) break;
            }
            else
                if (!string.IsNullOrEmpty(childName))
                {
                    var frameworkElement = child as FrameworkElement;

                    if (frameworkElement != null && frameworkElement.Name == childName)
                    {
                        foundChild = (T)child;
                        break;
                    }
                    else
                    {
                        foundChild = FindChild<T>(child, childName);

                        if (foundChild != null)
                        {
                            break;
                        }
                    }
                }
                else
                {
                    foundChild = (T)child;
                    break;
                }
        }

        return foundChild;
    }   
于 2013-08-07T08:10:44.107 に答える