0

Silverlight5 で、ユーザー コントロール リソース (button_click イベントから絵文字) を参照する方法は?

コード ビハインドでオブジェクトを構築し、その方法でデータ バインディングを行うと、オブジェクトに問題なくアクセスできますが、XAML アプローチを使用すると、より優れたツールが得られます。例はCh16から来ています

private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            // **HERE how do I reference the XAML emoticon object

            //emoticon.Name = "Smiley face2";
            //emoticon.Icon = new BitmapImage(new Uri("icons/happy.png", UriKind.RelativeOrAbsolute));
        }

.

<UserControl x:Class="EmoticonExample.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"
             xmlns:local="clr-namespace:EmoticonExample"
             mc:Ignorable="d"
             d:DesignHeight="300"
             d:DesignWidth="400">
    <UserControl.Resources>
        <local:Emoticon x:Key="emoticon"
                        Name="SmileyFace"
                        Icon="icons/happy.png" />
    </UserControl.Resources>

    <Grid x:Name="LayoutRoot"
          Margin="10"
          Background="White"
          DataContext="{StaticResource emoticon}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <TextBlock Text="Name:" />
        <TextBlock Text="Image:" Grid.Column="1" />

        <TextBox Name="myTextBox" Text="{Binding Name, Mode=TwoWay}" Grid.Row="1" />
        <Image Name="myImage" Source="{Binding Icon}" Stretch="None" Grid.Row="1" Grid.Column="1" />

        <Button Content="Button" HorizontalAlignment="Left" Margin="10,116,0,-92" Grid.Row="1" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
    </Grid>

</UserControl>

ここに画像の説明を入力

4

1 に答える 1

0

ユーザーコントロールに名前を付ける

<UserControl x:Class="EmoticonExample.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" 
             xmlns:local="clr-namespace:EmoticonExample" 
             mc:Ignorable="d" 
             x:Name="Example"
             d:DesignHeight="300" 
             d:DesignWidth="400"> 

</UserControl

次に、コードビハインドでそれを参照します。

var emoticon = Example.Resources["emoticon"] as Emoticon; // Im not sure of your data type
于 2012-10-17T13:21:30.607 に答える