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.










share|improve this question
























  • some reading for you: dotnetpattern.com/wpf-listview-binding
    – JohnB
    yesterday










  • Thanks that uses binding tho not x:bind
    – Paul Stanley
    yesterday















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.










share|improve this question
























  • some reading for you: dotnetpattern.com/wpf-listview-binding
    – JohnB
    yesterday










  • Thanks that uses binding tho not x:bind
    – Paul Stanley
    yesterday













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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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












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.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    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
































    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.






    share|improve this answer

























      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.






      share|improve this answer























        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered yesterday









        quanik

        647214




        647214






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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




















































































            Popular posts from this blog

            Coverage of Google Street View

            Full-time equivalent

            Surfing