How to use TemplateBinding within ContentPresenter #18397
Replies: 3 comments
-
I'm pretty sure i'm messing up where i define "siblings" and the combo boxes in my control theme, but i'm not sure how to fix it... <ControlTheme x:Key="{x:Type controls:ExpanderComboVariation}" TargetType="controls:ExpanderComboVariation">
<Setter Property="Template">
<ControlTemplate>
<controls:ExpanderComboBase PersonName="{TemplateBinding MyPersonName}">
<controls:ExpanderComboBase.Siblings>
<StackPanel>
<ComboBox ItemsSource="{TemplateBinding BrothersItemSource}"/>
<ComboBox ItemsSource="{TemplateBinding SistersItemSource}"/>
</StackPanel>
</controls:ExpanderComboBase.Siblings>
</controls:ExpanderComboBase>
</ControlTemplate>
</Setter>
</ControlTheme> |
Beta Was this translation helpful? Give feedback.
-
Is this issue related maybe? #13230 |
Beta Was this translation helpful? Give feedback.
-
Fixed it. I find avalonia's datacontext/template syntax really confusing, but stepping through the contentpresenter .cs file made it clear i was misunderstanding the template/content relationship. I needed to make the template be the actual controls, and the content be the 'Person' Data. control theme fix: <ControlTheme x:Key="{x:Type controls:ExpanderComboVariation}" TargetType="controls:ExpanderComboVariation">
<Setter Property="Template">
<ControlTemplate>
<controls:ExpanderComboBase Name="PART_BaseControl"
PersonName="{TemplateBinding MyPersonName}">
<controls:ExpanderComboBase.SiblingsTemplate>
<DataTemplate x:DataType="vm:Person">
<StackPanel>
<ComboBox ItemsSource="{Binding Brothers}"/>
<ComboBox ItemsSource="{Binding Sisters}"/>
</StackPanel>
</DataTemplate>
</controls:ExpanderComboBase.SiblingsTemplate>
</controls:ExpanderComboBase>
</ControlTemplate>
</Setter>
</ControlTheme> control theme code-behind fix (I set the Datacontext explicitly because i can't figure out the syntax to do it in the xaml) [TemplatePart("BaseControl", typeof(ExpanderComboBase))]
public class ExpanderComboVariation : TemplatedControl
{
private ExpanderComboBase? _baseControl;
public static readonly StyledProperty<string?> MyPersonNameProperty =
AvaloniaProperty.Register<ItemsControl, string?>(nameof(MyPersonName));
public string? MyPersonName
{
get => GetValue(MyPersonNameProperty);
set => SetValue(MyPersonNameProperty, value);
}
protected override void OnApplyTemplate(TemplateAppliedEventArgs args)
{
base.OnApplyTemplate(args);
_baseControl = args.NameScope.Find<ExpanderComboBase>("PART_BaseControl");
Debug.Assert(_baseControl != null);
_baseControl.Siblings = DataContext!;
}
} |
Beta Was this translation helpful? Give feedback.
-
I'm running into an issue with a templated control i've made, using another control that has a content presenter. It seems I'm using the content presenter in the wrong way and data context is being broken silently. I have a couple combo boxes that i want populated from data, but no matter what i do they always appear empty.
first off, here's the issue I'm seeing with empty combo boxes:

Here's the (simplified) control i'm using as a base, that has the content presenter:
Here's my custom control that uses the other control (and the contentpresenter), where i try to populate the combo boxes:
Here's where I use my custom control in the window:
And here's the window viewmodel:
Beta Was this translation helpful? Give feedback.
All reactions