Binding from MainWindow.xaml vs ResourceDictionary > ControlTemplate
Posted: 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.
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>