I have the following custom control,
public sealed class MediaFileItemControl : Control
{
public static readonly DependencyProperty MediaFileProperty =
DependencyProperty.Register(nameof(MediaFile), typeof(StorageFile), typeof(MediaFileItemControl), new PropertyMetadata(null));
public MediaFileItemControl()
{
DefaultStyleKey = typeof(MediaFileItemControl);
}
public StorageFile MediaFile
{
get => (StorageFile)GetValue(MediaFileProperty);
set => SetValue(MediaFileProperty, value);
}
}
the following style for it,
<!-- Generic.xaml -->
<Style TargetType="controls:MediaFileItemControl" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="controls:MediaFileItemControl">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{TemplateBinding MediaFile.Name}" />
<TextBlock Text="{TemplateBinding MediaFile.Path}" />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
and lastly, I want to use it within a ListBox, something like:
<ListBox ItemsSource="{x:Bind ViewModel.MediaFiles, Mode=OneWay}">
<ListBox.ItemTemplate>
<DataTemplate x:DataType="win_storage:StorageFile" xmlns:win_storage="using:Windows.Storage">
<controls:MediaFileItemControl MediaFile="{x:Bind}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
However, when I build the project, I get the following error in Generic.xaml
The XAML Binary Format (XBF) generator reported syntax error '0x80004005'
Where is the syntax error and what is the right way to achieve what I want?
<ControlTemplate><StackPanel Orientation="Horizontal"><TextBlock Text="{Binding Name}" /><TextBlock Text="{Binding Path}" /></StackPanel></ControlTemplate>Nameis for which property? By the way, I tried what you suggested before. The syntax error goes away, but the binding still does not work. The list item is there but there is nothing visual.TemplateBindingwithBindingas well. Yes, this works. I am a bit confuse though because of the magic (dynamic binding) this entails. Is there a way to usex:Bindinstead ofBindingto get compile time check?TemplateBindingdoesn't support multi-part paths. It's typically used to bind a property of a template part to the property of the templated control and it's the first time I see someone trying to bind it to a property of the property. Usually abindingorx:Bindto a list item is defined within aDataTemplateand a custom control should not be aware what item model someone is binding it to. You could put yourMediaFileItemControlin theDataTemplatebut, make MFIC derive fromContentControland put theStackPanel+ the bindings in theListBox.ItemTemplate