sgonchar
Topic Author
Posts: 48
Joined: 15 Mar 2021, 22:11

Binding from MainWindow.xaml vs ResourceDictionary > ControlTemplate

02 Aug 2022, 22:29

Hello,
We're running into odd behavior.

Things work when we have have: MainWIndow.xaml > view box > list box ItemsSource="{Binding listData}"
List doesn't show when we have MainWindow.xaml > new control : customList > Resource Dictionary > ControlTemplate > ListBox ItemsSource="{Binding listData, RelativeSource={RelativeSource TemplatedParent}}"

I'm wondering:
- What do I need to do for the ControlTemplate ListBox to recognize the binding correctly and pull data?
- Could empty code behind be a proble?
- Where can I breakpoint to try to debug something like this locally?

Working setup:
- View model has "listData" defined with getters / setters. We know there's data there.
<Window
  x:Name="MainWindow"
  xmlns=...
  >
  <Window.Resources>
    <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
        ...
        <ResourceDictionary Source="Panels/CustomList.xaml" />
      </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
  </Window.Resources>
  <Viewbox>
    <Grid Width="1920" Height="1080">
      <ListBox
        Width="600"   Margin="24,61"    HorizontalContentAlignment="Stretch"
        ScrollViewer.CanContentScroll="True"   ScrollViewer.VerticalScrollBarVisibility="Visible"
        ItemTemplate="{DynamicResource CustomLIstComponentStyle}"
        ItemsSource="{Binding listData}" />				// will show data
      <Panels:CustomList  x:Name="CustomList " />   			// will not show data
    </Grid>
  </Viewbox>
</Window>

Not working setup:
- View model has "listData" defined with getters / setters. We know there's data there.
- Code behind for customList is empty, exists because we plan to add to it later.
- MainWindow.xaml same as above.
- CustomList.xaml:
<ResourceDictionary
  x:Name="CustomList"
  xmlns= ...
  >
  <ControlTemplate x:Key="CustomList" TargetType="{x:Type Panels:CustomList}">
    <Grid
      Width="600" Height="1080" HorizontalAlignment="Right"
      Background="{StaticResource ColorBrush.gray.04-alpha.90}">
      <ListBox
        Margin="24,61" HorizontalContentAlignment="Stretch" 
        ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Visible"
        ItemTemplate="{DynamicResource CustomLIstComponentStyle}"
        ItemsSource="{Binding listData, RelativeSource={RelativeSource TemplatedParent}}" />   // What do I do here?
    </Grid>
  </ControlTemplate>
</ResourceDictionary>

 
User avatar
sfernandez
Site Admin
Posts: 2984
Joined: 22 Dec 2011, 19:20

Re: Binding from MainWindow.xaml vs ResourceDictionary > ControlTemplate

03 Aug 2022, 11:54

Hi, I think the binding path is not correct in the second case, because "{Binding listData, RelativeSource={RelativeSource TemplatedParent}}" means that Panels:CustomList control exposes a property named "listData", instead of referring to a property of the DataContext as you specified in the first case.

Have you tried with "{Binding DataContext.listData, RelativeSource={RelativeSource TemplatedParent}}"? And even simpler, if you don't change the DataContext in your custom control, it will inherit down the template tree, so you can just do "{Binding listData}".
 
sgonchar
Topic Author
Posts: 48
Joined: 15 Mar 2021, 22:11

Re: Binding from MainWindow.xaml vs ResourceDictionary > ControlTemplate

03 Aug 2022, 20:58

Hello!
Thank you, we tried the above, and the most simple case should have worked, worked everywhere else for us.
Finally figured it out .. the MainWindow.xaml '<Panels:CustomList x:Name="CustomList " /> ' just wasn't loading because CustomList.xaml was missing:
  <Style TargetType="{x:Type Panels:CustomList}">
    <Setter Property="Template" Value="{StaticResource CustomList}"/>
  </Style>
What does the above actually do?
Why does a template need to be set for CustomList for it to show up?
 
User avatar
sfernandez
Site Admin
Posts: 2984
Joined: 22 Dec 2011, 19:20

Re: Binding from MainWindow.xaml vs ResourceDictionary > ControlTemplate

04 Aug 2022, 20:57

The Template property default value is null, so if you don't specify one for the control style then nothing will be rendered for that control.

The previous xaml code is defining an implicit Style for all CustomList controls, so you don't have to manually set the Style property on each instance of your control. And it is defining its Template property, which is the minimum you need to specify the appearance of your control.

Who is online

Users browsing this forum: No registered users and 93 guests