开发者

Why ItemsControl item's DataContext assigned after they've been rendered

开发者 https://www.devze.com 2023-03-08 23:24 出处:网络
I have the following ItemsControl defined in my XAML: <UserControl.Resources> <DataTemplate x:Key=\"myDataTemplate\">

I have the following ItemsControl defined in my XAML:

<UserControl.Resources>
    <DataTemplate x:Key="myDataTemplate">
        <Grid Margin="20">

            <View:MyView Width="123" Height="212"/>

        </Grid>
    </DataTemplate>
</UserControl.Resources>

<Grid>
    <ItemsControl ItemsSource="{Binding MyVMs}" ItemTemplate="{StaticResource myDataTemplate}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel />
            </ItemsPanelTemplate>
        开发者_如何学编程</ItemsControl.ItemsPanel>
    </ItemsControl>
</Grid>

And DataContext of the View:MyView gets assigned after all UI elements where rendered. I am interested to know, why is that happening?


The DataContext is not set after the elements are rendered. If this was the case, you would see the your elements rendered twice, once initially when the DataContext is null, and the second time when the DataContext is set and your bindings are evaluated.

From what you are describing, you are breakpointing or tracing within the constructor. Properties cannot be set on a class before they are constructed!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号