UWP ListView DataTemplate Bindings
up vote
1
down vote
favorite
My ViewModel
BookingTakerViewModel
has a property
public List<string> Vias {get;set;}
I am wanting to bind a ListView with a DataTemplate but I can not find out how to do it. This is my code and it is throwing this error when it runs:
"Unable to cast object of type 'System.String' to type 'UI_Test_1.ViewModels.BookingTakerViewModel'."
I thought that the x:DataType should reference the class and in this case it is my Viewmodel.I presume that the x:Bind Vias is wrong beacuase this is a List so lost as to what to do.
<ListView
Name="viasList"
Width="300"
BorderBrush="Black"
BorderThickness="5"
ItemsSource="{x:Bind viewModel.Vias, Mode=OneWay}">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="MinHeight" Value="1" />
<Setter Property="MaxHeight" Value="15" />
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate x:Name="dt" x:DataType="viewmodels:BookingTakerViewModel">
<Grid>
<TextBlock FontSize="14" Text="{x:Bind Vias, Mode=OneWay}" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
The Vias are intantiated with data so it's not that which is causing a NullReference Exception. I believe the XAML is incorrect.
c# xaml uwp
add a comment |
up vote
1
down vote
favorite
My ViewModel
BookingTakerViewModel
has a property
public List<string> Vias {get;set;}
I am wanting to bind a ListView with a DataTemplate but I can not find out how to do it. This is my code and it is throwing this error when it runs:
"Unable to cast object of type 'System.String' to type 'UI_Test_1.ViewModels.BookingTakerViewModel'."
I thought that the x:DataType should reference the class and in this case it is my Viewmodel.I presume that the x:Bind Vias is wrong beacuase this is a List so lost as to what to do.
<ListView
Name="viasList"
Width="300"
BorderBrush="Black"
BorderThickness="5"
ItemsSource="{x:Bind viewModel.Vias, Mode=OneWay}">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="MinHeight" Value="1" />
<Setter Property="MaxHeight" Value="15" />
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate x:Name="dt" x:DataType="viewmodels:BookingTakerViewModel">
<Grid>
<TextBlock FontSize="14" Text="{x:Bind Vias, Mode=OneWay}" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
The Vias are intantiated with data so it's not that which is causing a NullReference Exception. I believe the XAML is incorrect.
c# xaml uwp
some reading for you: dotnetpattern.com/wpf-listview-binding
– JohnB
yesterday
Thanks that uses binding tho not x:bind
– Paul Stanley
yesterday
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
My ViewModel
BookingTakerViewModel
has a property
public List<string> Vias {get;set;}
I am wanting to bind a ListView with a DataTemplate but I can not find out how to do it. This is my code and it is throwing this error when it runs:
"Unable to cast object of type 'System.String' to type 'UI_Test_1.ViewModels.BookingTakerViewModel'."
I thought that the x:DataType should reference the class and in this case it is my Viewmodel.I presume that the x:Bind Vias is wrong beacuase this is a List so lost as to what to do.
<ListView
Name="viasList"
Width="300"
BorderBrush="Black"
BorderThickness="5"
ItemsSource="{x:Bind viewModel.Vias, Mode=OneWay}">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="MinHeight" Value="1" />
<Setter Property="MaxHeight" Value="15" />
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate x:Name="dt" x:DataType="viewmodels:BookingTakerViewModel">
<Grid>
<TextBlock FontSize="14" Text="{x:Bind Vias, Mode=OneWay}" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
The Vias are intantiated with data so it's not that which is causing a NullReference Exception. I believe the XAML is incorrect.
c# xaml uwp
My ViewModel
BookingTakerViewModel
has a property
public List<string> Vias {get;set;}
I am wanting to bind a ListView with a DataTemplate but I can not find out how to do it. This is my code and it is throwing this error when it runs:
"Unable to cast object of type 'System.String' to type 'UI_Test_1.ViewModels.BookingTakerViewModel'."
I thought that the x:DataType should reference the class and in this case it is my Viewmodel.I presume that the x:Bind Vias is wrong beacuase this is a List so lost as to what to do.
<ListView
Name="viasList"
Width="300"
BorderBrush="Black"
BorderThickness="5"
ItemsSource="{x:Bind viewModel.Vias, Mode=OneWay}">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="MinHeight" Value="1" />
<Setter Property="MaxHeight" Value="15" />
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate x:Name="dt" x:DataType="viewmodels:BookingTakerViewModel">
<Grid>
<TextBlock FontSize="14" Text="{x:Bind Vias, Mode=OneWay}" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
The Vias are intantiated with data so it's not that which is causing a NullReference Exception. I believe the XAML is incorrect.
c# xaml uwp
c# xaml uwp
edited yesterday
quanik
647214
647214
asked yesterday
Paul Stanley
551520
551520
some reading for you: dotnetpattern.com/wpf-listview-binding
– JohnB
yesterday
Thanks that uses binding tho not x:bind
– Paul Stanley
yesterday
add a comment |
some reading for you: dotnetpattern.com/wpf-listview-binding
– JohnB
yesterday
Thanks that uses binding tho not x:bind
– Paul Stanley
yesterday
some reading for you: dotnetpattern.com/wpf-listview-binding
– JohnB
yesterday
some reading for you: dotnetpattern.com/wpf-listview-binding
– JohnB
yesterday
Thanks that uses binding tho not x:bind
– Paul Stanley
yesterday
Thanks that uses binding tho not x:bind
– Paul Stanley
yesterday
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
Probably you are looking for something like this?
<ListView.ItemTemplate>
<DataTemplate x:Name="dt" x:DataType="x:String">
<Grid>
<TextBlock FontSize="14" Text="{x:Bind}" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
Inside the data template, the context of {x:Bind} is the templated object, which is a string item from the collection you set in ItemsSource.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Probably you are looking for something like this?
<ListView.ItemTemplate>
<DataTemplate x:Name="dt" x:DataType="x:String">
<Grid>
<TextBlock FontSize="14" Text="{x:Bind}" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
Inside the data template, the context of {x:Bind} is the templated object, which is a string item from the collection you set in ItemsSource.
add a comment |
up vote
2
down vote
accepted
Probably you are looking for something like this?
<ListView.ItemTemplate>
<DataTemplate x:Name="dt" x:DataType="x:String">
<Grid>
<TextBlock FontSize="14" Text="{x:Bind}" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
Inside the data template, the context of {x:Bind} is the templated object, which is a string item from the collection you set in ItemsSource.
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Probably you are looking for something like this?
<ListView.ItemTemplate>
<DataTemplate x:Name="dt" x:DataType="x:String">
<Grid>
<TextBlock FontSize="14" Text="{x:Bind}" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
Inside the data template, the context of {x:Bind} is the templated object, which is a string item from the collection you set in ItemsSource.
Probably you are looking for something like this?
<ListView.ItemTemplate>
<DataTemplate x:Name="dt" x:DataType="x:String">
<Grid>
<TextBlock FontSize="14" Text="{x:Bind}" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
Inside the data template, the context of {x:Bind} is the templated object, which is a string item from the collection you set in ItemsSource.
answered yesterday
quanik
647214
647214
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237837%2fuwp-listview-datatemplate-bindings%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
some reading for you: dotnetpattern.com/wpf-listview-binding
– JohnB
yesterday
Thanks that uses binding tho not x:bind
– Paul Stanley
yesterday