C# 如何使用MVVM将数据正确绑定到WPF按钮
这是我的设置,似乎我没有正确绑定数据。结果应该是显示图像和文本的按钮。没有显示任何内容C# 如何使用MVVM将数据正确绑定到WPF按钮,c#,wpf,mvvm,C#,Wpf,Mvvm,这是我的设置,似乎我没有正确绑定数据。结果应该是显示图像和文本的按钮。没有显示任何内容 <UserControl x:Class="AmebaPrototype.UI.LandingPivot.featureControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/x
<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 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;
}
}
如果确实希望使用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中,您可以调整它以包含图像和文本块。参考资料中的DataTemplate:
<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>
我添加了一个命令,因为如果您想实现MVVM而不是事件单击,这是必需的。您的
VideoViewModel
看起来怎么样?因为在UserControl
的DataContext
中,您绑定到了VideoViewModel
,它不是Video
类本身,而是包含(我猜)一个类型为Video
的属性。运行程序时,请检查此链接以使用MVVM与按钮绑定,请查看Visual Studio中的输出面板。有BindingExpression错误吗?@nemesv我刚刚用ViewModel@GregSansom不,没有错误我以前用ListView和ListBox实现过,但现在我只需要实现一个按钮。i、 e没有列表视图或其他布局。什么是x?参考。我试过了,但它不允许我将其设置为x:Type。
<ListView
ItemsSource="{Binding ListData}"
ItemTemplate="{StaticResource bTemplate}"
>
</ListView>