I am working with the WPF project, it's kinda annoying to have the same repetitive exception handling that only number makes it different?
Here's the code:
try
{
filling_head_model1.Text = models[0].Model;
filling_head_type1.Text = "1. " + models[0].Type;
filling_head_subtype1.Text = models[0].SubType;
filling_head_image1.Source = defaultImage;
}
catch (IndexOutOfRangeException)
{
filling_head_model1.Text = null;
filling_head_type1.Text = null;
filling_head_subtype1.Text = null;
filling_head_image1.Source = null;
}
try
{
filling_head_model2.Text = models[1].Model;
filling_head_type2.Text = "2. " + models[1].Type;
filling_head_subtype2.Text = models[1].SubType;
filling_head_image2.Source = defaultImage;
}
catch (IndexOutOfRangeException)
{
filling_head_model2.Text = null;
filling_head_type2.Text = null;
filling_head_subtype2.Text = null;
filling_head_image2.Source = null;
}
... // there are 5 more
As you can see the only thing changed is the number at the back that represents each component. I know how to change the index of the array using loops, but how do I change the number at the back of the component name programmatically?
You don't need the try/catch
at all.
Just use an ItemsControl with an appropriate ItemTemplate.
<ItemsControl x:Name="itemsControl">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Model}"/>
<TextBlock>
<Run Text="{Binding Number}"/><Run Text="."/>
<Run Text="{Binding Type}"/>
</TextBlock>
<TextBlock Text="{Binding SubType}"/>
<Image Source="{Binding Image}"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
and assign the models
collection to its ItemsSource:
itemsControl.ItemsSource = models;
Note that the model members must be public properties.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.