【问题标题】:Windows Phone Template and ButtonWindows Phone 模板和按钮
【发布时间】:2012-05-29 08:07:41
【问题描述】:

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

我有一个扩展 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"  />

谢谢

【问题讨论】:

    标签: c# templates button windows-phone


    【解决方案1】:
    <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); 
    

    希望有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-02
      • 2012-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多