I'm making a bingo card generator to try and learn more about WPF, and having trouble figuring out how to set a labels content property to be set from a Property in my code-behind file.
I thought that I could use
<Setter Property="Content" Value="{Binding BNumber}">
for the content property to set the content of the label to a random element of my List<String>
?
I have in my 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>
My App.xaml code
<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>
In my MainWindow.xaml.cs I have this List<String> BNumbers
object and a Property that returns a random element of the BNumbers
List
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>();