0

I'm trying to use a UserControl-derived control (UpdateBlockControl) inside the DataTemplate of a ListView, like this:

    <ListView x:Name="AppsListView">
       <ListView.ItemTemplate>
          <DataTemplate x:DataType="model:AppBase">
             <StackPanel Orientation="Horizontal">
                <local:UpdateBlockControl ImageSource="{x:Bind ImageSource}" AppName="{x:Bind Name}"/>
                <TextBlock Text="{x:Bind Name}" Foreground="Orange" Margin="8,0,0,0" />
                <TextBlock Text="{x:Bind ImageSource}" Margin="8,0,0,0" Foreground="LimeGreen"/>
             </StackPanel>
          </DataTemplate>
       </ListView.ItemTemplate>
    </ListView>

UpdateBlockControl is derived from UserControl and I'm trying to bind its properties (ImageSource and AppName) to the model's (AppBase) properties.

You can see me trying to set up these bindings in the first item in the StackPanel. Just to see if the binding works at all, I also bind a couple of TextBlocks to these properties.

The XAML for UserBlockControl is essentially this:

    <StackPanel Orientation="Vertical">
        <TextBlock Text=":-)"/>
        <TextBlock Text="{x:Bind AppName}"/>
        <TextBlock Text="{x:Bind ImageSource}"/>
    </StackPanel>

When I run the application I see the following:

enter image description here

So the UserControl properties aren't working correctly it would seem: I get the :-) for the first TextBlock but the second and third TextBlocks are empty. Their equivalents in the ListView do work however (see image, above).

Should I be able to use a user control insisde a DataTemplate like this? If so, why aren't the bindings working?

2
  • Is this a WinUI 3 app? Commented Oct 8, 2022 at 2:44
  • @AndrewKeepCoding It is. Commented Oct 8, 2022 at 21:29

1 Answer 1

1

x:Bind is Mode=OneTime by default. So, once a DependencyProperty is initialized inside the UserControl, it won't be updated anymore.

You need to add Mode=OneWay to x:Bind in UpdateBlockControl.

<StackPanel Orientation="Vertical">
    <TextBlock Text=":-)" />
    <TextBlock Text="{x:Bind AppName, Mode=OneWay}" />
    <TextBlock Text="{x:Bind ImageSource, Mode=OneWay}" />
</StackPanel>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that did the trick. I was also missing INotifyPropertyChanged from the user control. Doesn't seem happy when that's missing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.