Can't do x:Bind for DataTemplate

  <models:EffectModel x:Name="EffectModel" x:Key="EffectModel"/> 

...

 <ListView.ItemTemplate> <DataTemplate x:DataType="{StaticResource EffectModel}"> <TextBlock Text="{x:Bind Title}"/> </DataTemplate> </ListView.ItemTemplate> 

Title is a public string property. Constantly writes on Title that it is not in the context of MainPage . Why he does not address to EffectModel

 public class EffectModel { public string Title { get; set; } public Uri FrameImage { get; set; } } 

UPD

  <DataTemplate x:DataType="models:EffectModel"> <Border Width="400" Height="240"> <Border.Background> <ImageBrush Stretch="Fill" ImageSource="{x:Bind FrameImage}"/> </Border.Background> </Border> </DataTemplate> 
  • Hm And why x:DataType="{StaticResource EffectModel}" and not x:DataType="models:EffectModel" ? Maybe this is the problem? - VladD
  • So he seems to have already been described and I am appealing to the key, is it wrong? - SmiLe
  • 2
    No, {StaticResource EffectModel} - this is not the type! This is an object. And you need exactly the type. - VladD
  • @VladD, updated the question. For pictures does not work, says you need a converter. Normal Binding works without any converters - SmiLe

1 answer 1

In the case of x:Bind , as the user VladD suggested, you must specify x:DataType="model: your model in which the property is located. " .

Converter Code:

 public sealed class ImageConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter,string culture) { if (value is string) return new BitmapImage(new Uri((string)value, UriKind.RelativeOrAbsolute)); if (value is Uri) return new BitmapImage((Uri)value); throw new NotSupportedException(); } public object ConvertBack(object value, Type targetType, object parameter, string culture) { throw new NotSupportedException(); } } 

On Xaml will look like this:

 ImageSource="{x:Bind FrameImage, Converter={StaticResource ImageConverter}}"/> 

ImageConvertor declared in App.xaml

  • You can do without a converter if the FrameImage is of type string, not Uri. - Make Makeluv