1

我对自定义控件及其模板有疑问。

我有一个扩展 Button 的控件,它的模板是通过 Expression Blend 4 创建的,它在 App.xaml 的资源中可用。

这是模板:

<ControlTemplate x:Key="ImageButtonControlTemplate1" TargetType="maquette_v0__1_Controles_persos:ImageButton">
        <Grid Margin="0" Width="150">
            <VisualStateManager.VisualStateGroups>
                <VisualStateGroup x:Name="CommonStates">
                    <VisualState x:Name="Disabled"/>
                    <VisualState x:Name="Normal">
                        <Storyboard>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="image">
                                <DiscreteObjectKeyFrame KeyTime="0">
                                    <DiscreteObjectKeyFrame.Value>
                                        <Visibility>Collapsed</Visibility>
                                    </DiscreteObjectKeyFrame.Value>
                                </DiscreteObjectKeyFrame>
                            </ObjectAnimationUsingKeyFrames>
                        </Storyboard>
                    </VisualState>
                    <VisualState x:Name="MouseOver"/>
                    <VisualState x:Name="Pressed">
                        <Storyboard>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="image1">
                                <DiscreteObjectKeyFrame KeyTime="0">
                                    <DiscreteObjectKeyFrame.Value>
                                        <Visibility>Collapsed</Visibility>
                                    </DiscreteObjectKeyFrame.Value>
                                </DiscreteObjectKeyFrame>
                            </ObjectAnimationUsingKeyFrames>
                        </Storyboard>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
            <Image x:Name="image1" Source="{TemplateBinding Image}" Height="150"/>
            <Image x:Name="image" Source="{TemplateBinding PressedImage}" Height="150"/>
        </Grid>
    </ControlTemplate>

在一个页面中,我想动态添加一个自定义控件。为此,我的文件 Xaml“menu_commune”中有一个网格,我想在这个网格中显示我的控件。

这是代码 C#:

ImageButton b = new ImageButton();
b.SetValue(Grid.RowProperty, 0);
b.SetValue(Grid.ColumnProperty, 2);
// bool t = Application.Current.Resources["ImageButtonControlTemplate1"] == null;
ControlTemplate ctemp = (ControlTemplate)    Application.Current.Resources["ImageButtonControlTemplate1"];
b.Template = ctemp;
BitmapImage bi_noPress = new BitmapImage(new Uri("/Images/menu_agenda.png"));
BitmapImage bi_Press = new BitmapImage(new Uri("/Images/menu_agenda_selected.png"));
b.Image = bi_noPress;
b.PressedImage = bi_Press;
menu_commune.Children.Add(b);

问题是什么都没有显示。但是,Xaml 中的声明运行良好。

<UC:ImageButton Grid.Row="2" Grid.Column="2" Image="../Images/menu_office-de-tourisme.png" PressedImage="../Images/menu_office-de-tourisme_selected.png" Height="150" Template="{StaticResource ImageButtonControlTemplate1}" Click="tourisme_Click"  />

谢谢

4

1 回答 1

0
<UC:ImageButton Grid.Row="2" Grid.Column="2" Image="../Images/menu_office-de-tourisme.png" PressedImage="../Images/menu_office-de-tourisme_selected.png" Height="150" Template="{StaticResource ImageButtonControlTemplate1}" Click="tourisme_Click"  />

在这里您使用Grid.Row="2" Grid.Column="2", 并且在代码中您需要使用相同的值。代替

b.SetValue(Grid.RowProperty, 0);
b.SetValue(Grid.ColumnProperty, 2);

为了这

b.SetValue(Grid.RowProperty, 2);
b.SetValue(Grid.ColumnProperty, 2); 

希望它的帮助。

于 2013-04-12T07:02:43.013 回答