我正在创建一个小示例,以在 WPF 页面上使用 ViewModel 显示枚举数据。但不知何故它没有显示。
public class GameParticipants: IGameParticipants
{
public int Id{ get; set; }
public List<string> Name { get; set; }
public Dictionary<int,string> GameWinner { get; set; }
public DateTime WinningDate { get; set; }
}
public interface IGameParticipants
{
List<string> Name { get; set; }
Dictionary<int, string> GameWinner { get; set; }
DateTime WinningDate { get; set; }
}
ParticipantsViewModel.cs
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string prop)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
}
}
public class ParticipantsViewModel : ViewModelBase
{
public string RemoveUnderscore(string text)
{
return text.Replace("_", " ");
}
public ParticipantsViewModel()
{
Participants = new GamesParticipants()
{
Name = new List<string>()
{
Handball.ToString(),
Football.ToString(),
Cricket.ToString(),
Badminton.ToString(),
Tennis.ToString(),
TableTenis.ToString(),
Rugby.ToString(),
Boxing.ToString(),
}
};
}
private GamesParticipants _participants;
public GamesParticipants Participants
{
get { return _participants; }
set
{
_participants = value;
RaisePropertyChanged("Participants");
}
}
}
MainWindow.xaml
<Window x:Class="Games.MainWindow"
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:Games"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.DataContext>
<local:ParticipantsViewModel/>
</Grid.DataContext>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<ListView ItemsSource="{Binding Path=Participants}" x:Name="lstParticipants" Grid.Row="1" Grid.Column="1"
VerticalContentAlignment="Top" ScrollViewer.CanContentScroll="True" d:ItemsSource="{d:SampleData ItemCount=13}">
<ListView.ItemTemplate>
<DataTemplate>
<Viewbox>
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" Orientation="Horizontal">
<TextBlock Grid.Row="0" Text="{Binding Name}" TextWrapping="Wrap" FontWeight="Bold" FontSize="40" Foreground="Black" />
</StackPanel>
</Viewbox>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Button x:Name="btnRandom" Grid.Row="2" Content="First Game" FontSize="45" Foreground="Green" Cursor="Hand"
Grid.Column="1" VerticalAlignment="Center" Click="btnRandom_Click"></Button>
</Grid>
MainWindow.xaml 的 UI
如果要显示在视图模型中创建的字符串(
Handball.ToString()
、Football.ToString()
等),则应绑定到 Name
对象的 GamesParticipants
属性:
<ListView ItemsSource="{Binding Path=Participants.Name}" x:Name="lstParticipants" Grid.Row="1" Grid.Column="1"
VerticalContentAlignment="Top" ScrollViewer.CanContentScroll="True" d:ItemsSource="{d:SampleData ItemCount=13}">
<ListView.ItemTemplate>
<DataTemplate>
<Viewbox>
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" Orientation="Horizontal">
<TextBlock Grid.Row="0" Text="{Binding}" TextWrapping="Wrap" FontWeight="Bold" FontSize="40" Foreground="Black" />
</StackPanel>
</Viewbox>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>