Binding from MainWindow.xaml vs ResourceDictionary > ControlTemplate
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.
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:
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.
Code: Select all
<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:
Code: Select all
<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>
-
sfernandez
Site Admin
- Posts: 2984
- Joined:
Re: Binding from MainWindow.xaml vs ResourceDictionary > ControlTemplate
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}".
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}".
Re: Binding from MainWindow.xaml vs ResourceDictionary > ControlTemplate
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:
What does the above actually do?
Why does a template need to be set for CustomList for it to show up?
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:
Code: Select all
<Style TargetType="{x:Type Panels:CustomList}">
<Setter Property="Template" Value="{StaticResource CustomList}"/>
</Style>
Why does a template need to be set for CustomList for it to show up?
-
sfernandez
Site Admin
- Posts: 2984
- Joined:
Re: Binding from MainWindow.xaml vs ResourceDictionary > ControlTemplate
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.
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: Ahrefs [Bot] and 9 guests