0

这是我的设置,看来我没有正确绑定数据。结果应该是一个显示图像和文本的按钮。什么都没有显示。

<UserControl x:Class="AmebaPrototype.UI.LandingPivot.featureControl"
         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" 
         d:DesignHeight="330" d:DesignWidth="960"
         DataContext="{Binding Source={StaticResource viewModelLocator}, Path=VideoViewModel}"
         >
<UserControl.Resources >
    <DataTemplate x:Key="custTemplate" >
        <StackPanel >
            <Image Source="{Binding ImagePath}"/>
            <TextBlock Text="{Binding MyTitle}"/>
        </StackPanel>          
    </DataTemplate>
</UserControl.Resources>

<Grid x:Name="LayoutRoot" Background="{x:Null}" >
    <Button Content="Button" Height="316" 
            HorizontalAlignment="Left" Margin="12,2,0,0" 
            Name="button1" VerticalAlignment="Top" 
            Width="423" Click="button1_Click" 
            ContentTemplate="{DynamicResource custTemplate}"
            />
    <Button Content="Button" Height="156" HorizontalAlignment="Left" Margin="441,2,0,0" Name="button2" VerticalAlignment="Top" Width="208" Click="button2_Click" />
    <Button Content="Button" Height="156" HorizontalAlignment="Left" Margin="669,2,0,0" Name="button3" VerticalAlignment="Top" Width="208" />
    <Button Content="Button" Height="156" HorizontalAlignment="Left" Margin="441,162,0,0" Name="button4" VerticalAlignment="Top" Width="208" />
    <Button Content="Button" Height="156" HorizontalAlignment="Left" Margin="669,162,0,0" Name="button5" VerticalAlignment="Top" Width="208" />
</Grid>


模型:

public class Video
    {
        public string  MyTitle { get; set; }
        public string ImagePath { get; set; }
    }

视图模型

  public ViewModel VideoViewModel 
    {
        get 
        {
            if(viewmodel == null)
            {
                viewmodel = new ViewModel();
                viewmodel.ListData.Clear();
                viewmodel.ListData.Add(new Video { MyTitle = "Title1", ImagePath = "exampleimage.com/image.png" });
                 viewmodel.ListData.Add(new Video { MyTitle = "Title2", ImagePath = "exampleimage.com/image.png" });

            }

            return viewmodel;
        }
    }
4

2 回答 2

1

如果您真的想使用 DataTemplate 执行此操作,则:

<Button>
        <Button.Content>
            <x:Type TypeName="Visual"/>
        </Button.Content>
        <Button.Template>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid>
                    <ContentPresenter ContentSource="Content"/>
                </Grid>
            </ControlTemplate>
        </Button.Template>
        <Button.ContentTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Path=Name}"/>
            </DataTemplate>
        </Button.ContentTemplate>
    </Button>

您的 ViewModel 绑定应该可以在这里使用,因此在 DataTemplate 中您可以调整它以包含您的 Image 和 TextBlock。

于 2012-06-26T05:15:05.273 回答
0

资源中的数据模板:

<DataTemplate x:Key="bTemplate">
        <Button Label="{Binding MyTitle}" 
                Command="{Binding Execute}"> 
           <Image Source="{Binding ImagePath}"   />
        </Button>
 </DataTemplate>

与您的问题不完全一样,但是您可以使用带有 ItemSource 的控件,例如 ListView:

<ListView 
  ItemsSource="{Binding ListData}"  
  ItemTemplate="{StaticResource bTemplate}" 
>
</ListView>

我添加了一个 Command ,因为如果你想实现 MVVM 而不是事件点击,这是必要的。

于 2012-06-26T14:27:58.090 回答