Here’s my ListBox
:
<ListBox x:Name="Difetti"
ItemsSource="{Binding Difetti}"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Margin="20,40,0,0"
Width="700"
Height="350"
Grid.Row="2"
Grid.ColumnSpan="3">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBox x:Name="Metro"
controls:TextBoxHelper.Watermark="Metro"
PreviewTextInput="validateMaskDecimal">
<TextBox.Text>
<Binding Path="Metro">
</Binding>
</TextBox.Text>
</TextBox>
<ComboBox x:Name="Grado"
ItemsSource="{Binding ListGradi}"
DisplayMemberPath="DisplayValue"
SelectedValuePath="Value"
SelectedValue="{Binding Grado}"
controls:TextBoxHelper.Watermark="Grado" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Which using a button I increase basically this way (empty record):
Difetto difetto = new Difetto();
data.Difetti.Add(difetto);
Once added, is there any PreviewAddItem
event? I need to set carefully the Watermark on each element, due to the current/selected language.
When I switch language, I iterate on each ListBox item’s doing somethings like this:
Utilities.FindVisualChild<TextBox>(listBoxItem, "Metro")?.SetValue(TextBoxHelper.WatermarkProperty, resourceManager?.GetString("Metro"));
Utilities.FindVisualChild<ComboBox>(listBoxItem, "Grado")?.SetValue(TextBoxHelper.WatermarkProperty, resourceManager?.GetString("Grado"));
But on create, I won’t to iterate the existing items only for set the new records. Is there somethings like this event? Or maybe I can get the ListBoxItem immediately after data.Difetti.Add(difetto)
before its being rendered on the UI?
For completeness, here’s the FindVisualChild
method:
public static T? FindVisualChild<T>(DependencyObject parent, string name) where T : FrameworkElement
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(parent, i);
if (child != null && child is T && ((T)child).Name == name)
{
return (T)child;
}
else if (child != null)
{
T? childOfChild = FindVisualChild<T>(child, name);
if (childOfChild != null)
{
return childOfChild;
}
}
}
return null;
}