我制作一个宾果卡生成器,试图了解更多有关WPF的信息,并且无法确定如何设置标签内容属性,以便在我的代码隐藏文件中设置属性。
我以为我可以使用
<Setter Property="Content" Value="{Binding BNumber}">
for content属性将标签的内容设置为List<String>
的随机元素?
我的 MainWindow.xaml
<Window x:Class="Bingo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="600" Width="800"
WindowStartupLocation="CenterScreen">
<Grid>
<Grid Width="350" Height="420" ShowGridLines="True">
<Grid.RowDefinitions>
<RowDefinition Height="70"/>
<RowDefinition Height="70"/>
<RowDefinition Height="70"/>
<RowDefinition Height="70"/>
<RowDefinition Height="70"/>
<RowDefinition Height="70"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="70" />
<ColumnDefinition Width="70" />
<ColumnDefinition Width="70" />
<ColumnDefinition Width="70" />
<ColumnDefinition Width="70" />
</Grid.ColumnDefinitions>
<!-- The Label I'm trying to set in this example -->
<Label Grid.Column="0" Grid.Row="1" Style="{StaticResource BNumber}"
FontSize="50" Width="70"/>
</Grid>
</Grid>
</Window>
我的 App.xaml 代码
<Application x:Class="Bingo.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style TargetType="Label" x:Key="BNumber">
<Setter Property="Content" Value="{Binding}"></Setter>
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="Beige"/>
</Setter.Value>
</Setter>
</Style>
</Application.Resources>
</Application>
在我的 MainWindow.xaml.cs 中,我有一个List<String> BNumbers
对象和一个返回BNumbers
列表
public MainWindow() {
InitializeComponent();
BNumbers.Add("1");
BNumbers.Add("2");
BNumbers.Add("3");
BNumbers.Add("4");
BNumbers.Add("5");
BNumbers.Add("6");
BNumbers.Add("7");
BNumbers.Add("8");
BNumbers.Add("9");
BNumbers.Add("10");
BNumbers.Add("11");
BNumbers.Add("12");
BNumbers.Add("13");
BNumbers.Add("14");
BNumbers.Add("15");
}
public string RandomBNumber {
get { return randomB(); }
}
public string randomB() {
Random rand = new Random();
int randomBNumber = rand.Next(0, 15);
return BNumbers[randomBNumber];
}
public List<String> BNumbers = new List<string>();
答案 0 :(得分:3)
可能更容易随机化列表本身然后每个数字,因为这将停止重复。
Aslo使用Uniform网格可能更容易添加一堆标签。
示例:
的Xaml:
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="346" Width="300" Name="UI">
<Grid DataContext="{Binding ElementName=UI}">
<ItemsControl ItemsSource="{Binding BNumbers}" Margin="0,29,0,0">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Margin="1" BorderBrush="Black" BorderThickness="1" CornerRadius="2">
<Label Content="{Binding}" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" />
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="5" Rows="5"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
<Button Content="New" Height="23" HorizontalAlignment="Left" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
</Window>
代码:
public partial class MainWindow : Window, INotifyPropertyChanged
{
public List<string> AllNumbers { get; set; }
private List<string> _bnumbers = new List<string>();
public MainWindow()
{
InitializeComponent();
AllNumbers = new List<string>();
// Bingo game 75 numbers, 5x5 grid
for (int i = 0; i < 75; i++)
{
AllNumbers.Add(i.ToString());
}
}
public List<string> BNumbers
{
get { return _bnumbers; }
set { _bnumbers = value; NotifyPropertyChanged("BNumbers"); }
}
private void button1_Click(object sender, RoutedEventArgs e)
{
RandomizeList(AllNumbers);
BNumbers = AllNumbers.Take(25).ToList();
}
private void RandomizeList<T>(IList<T> list)
{
Random rng = new Random();
int n = list.Count;
while (n > 1)
{
n--;
int k = rng.Next(n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
结果(“新”将生成新游戏)
答案 1 :(得分:2)
在构造函数中,您需要设置DataContext:
this.DataContext = this;
您还需要更改您的setter以匹配属性名称RandomBNumber:
<Setter Property="Content" Value="{Binding RandomBNumber }">