Hello @BitSmithy ,
Welcome to Microsoft Q&A!
Thank you for providing the complete code. This unexpected behavior has nothing to do with the binding method, you can use x:Bind and Binding. The reason is that Columns is empty and x:bind cannot find the corresponding value in Data context.
When adding a new FinancialItem
, your code only adds fiToAdd
, and the other local variable fi
does not seem to be used.
<ListView x:Name="lvExpAndCosts" ItemsSource="{x:Bind ocExpendituresAndCosts}">
<ListView.ItemTemplate>
<DataTemplate x:DataType="local:FinancialItem">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding Columns[0], Mode=OneWay}"></ColumnDefinition>
<ColumnDefinition Width="{Binding Columns[1], Mode=OneWay}"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBox Text="{Binding Name}" Grid.Column="0"
BorderThickness="1"
></TextBox>
<TextBox Text="{Binding Amount}" Grid.Column="1"
TextAlignment="Right"
BorderThickness="1"
></TextBox>
<Button x:Name="btnAddFinancialItem" Content="+" Margin="10,0,0,0" Grid.Column="2"
Click="btnAddFinancialItem_Click"></Button>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
private void btnAddFinancialItem_Click(object sender, RoutedEventArgs e)
{
if (sender is Button btn && btn.DataContext is FinancialItem fi)
{
int rowIndexToAdd = lvExpAndCosts.IndexFromContainer(lvExpAndCosts.ContainerFromItem(fi)) + 1;
//define default financial item
FinancialItem fiToAdd = new FinancialItem() { Name = "Test", Amount = 0 };
fiToAdd.Columns.Add(new GridLength(100, GridUnitType.Pixel));
fiToAdd.Columns.Add(new GridLength(100));
fiToAdd.Columns.Add(new GridLength(100));
ocExpendituresAndCosts.Insert(rowIndexToAdd, fiToAdd);
}
}
Thank you.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.