Using ItemsControl with ExtendedDataConverter

6 posts, 1 answers
  1. Jason D
    Jason D avatar
    72 posts
    Member since:
    Jan 2011

    Posted 14 Aug 2014 Link to this post

    I am creating a ToolTipTemplate in code based on this previous thread:
    http://www.telerik.com/forums/programmtically-changing-mapshapereader-tooltip-when-using-a-custom-template

    That works. However, the underlying MapShapeData represents information from an unknown number of values. Example:

    Value 1

    or maybe:

    Value 1
    Value 2
    Value 3

    etc.

    Is it possible to bind an ItemsControl to list all these values without knowing in advance how many there are?
  2. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 15 Aug 2014 Link to this post

    Hi Jason,

    Basically you can place everything in the ToolTipTemplate - ListBox or any ItemsControl , etc. Of course, then you need to bind its ItemsSource property to some IEnumerable. If these values (Value 1 Value 2 Value 3) are stored in a List in the shape file and the list name is "Values", the binding would look like:
    <ListBox                               ItemTemplate={StaticResource yourListBoxItemTemplate}
                                                  ItemsSource="{Binding Converter={StaticResource ExtendedDataConverter}, ConverterParameter='Values'}" />

    However, I think the supported formats in the shp and dbf files  are only floating point, integer, date and text. In other words the reader cannot return directly IEnumerable.

    Let's suppose your values are stored in a string (text) named "Values". A possible solution is to create a class which inherits from our ExtendedDataConverter.  In the Convert method, "base.Convert" with parameter "Values" will give you a string read from the shp or dbf file. Then you can split this string and return List<string> or other type of IEnumerable<string> which can be passed to the ItemsSource of the RadListBox.

    If this is not your scenario, could you please elaborate a bit more on it - for instance how is your list stored? Thank you in advance for your cooperation.

    Regards,
    Petar Mladenov
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  3. UI for WPF is Visual Studio 2017 Ready
  4. Jason D
    Jason D avatar
    72 posts
    Member since:
    Jan 2011

    Posted 15 Aug 2014 in reply to Petar Mladenov Link to this post

    That makes sense, but I cannot get it to work. I am creating the tooltips manually, I am not using shape files. So in this case, I am just creating a List(Of String). The generated XAML looks correct and works with a test application which binds directly to a List. I wasn't sure how to add another template, so I embedded it in the other one. Here is my code:

    Private Function CreateTooltipTemplate(ByVal propertyName As String) As Windows.DataTemplate
        Dim textBinding1 As String = "{Binding Converter={StaticResource ExtendedDataConverter}," & "ConverterParameter='" & propertyName & "'}"
     
        Dim textBinding2 As String = "{Binding Converter={StaticResource ExtendedDataConverter},ConverterParameter='values'}"
     
        Dim xns As Xml.Linq.XNamespace = "http://schemas.microsoft.com/winfx/2006/xaml"
        Dim telerik As Xml.Linq.XNamespace = "http://schemas.telerik.com/2008/xaml/presentation"
     
        Dim loItemTemplate As New Xml.Linq.XElement("DataTemplate", _
         New Xml.Linq.XElement("TextBlock", _
          New Xml.Linq.XAttribute("FontWeight", "Bold"), _
          New Xml.Linq.XAttribute("FontFamily", "Trebuchet MS"), _
          New Xml.Linq.XAttribute("FontSize", "12"), _
          New Xml.Linq.XAttribute("Text", "{Binding}")))
     
        Dim dataTemplateElement As New Xml.Linq.XElement("DataTemplate", _
         New Xml.Linq.XElement("StackPanel", _
          New Xml.Linq.XAttribute("Margin", "3,3"), _
          New Xml.Linq.XElement("StackPanel.Resources", _
        New Xml.Linq.XElement(telerik + "ExtendedDataConverter", _
         New Xml.Linq.XAttribute(xns + "Key", "ExtendedDataConverter"))), _
          New Xml.Linq.XElement("TextBlock", _
        New Xml.Linq.XAttribute("FontWeight", "Bold"), _
        New Xml.Linq.XAttribute("FontFamily", "Trebuchet MS"), _
        New Xml.Linq.XAttribute("FontSize", "12"), _
        New Xml.Linq.XAttribute("Text", textBinding1)), _
          New Xml.Linq.XElement("ItemsControl", _
        New Xml.Linq.XAttribute("ItemsSource", textBinding2), _
        New Xml.Linq.XElement("ItemsControl.ItemTemplate", loItemTemplate))))
     
        Dim dataTemplateXaml As String = dataTemplateElement.ToString()
        Dim sr As New IO.MemoryStream(System.Text.Encoding.ASCII.GetBytes(dataTemplateXaml))
     
        Dim pc As New Windows.Markup.ParserContext()
        pc.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation")
        pc.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml")
        pc.XmlnsDictionary.Add("telerik", "http://schemas.telerik.com/2008/xaml/presentation")
     
        Dim dataTemplate As Windows.DataTemplate = DirectCast(Windows.Markup.XamlReader.Load(sr, pc), Windows.DataTemplate)
     
        Return dataTemplate
    End Function

    This code generates the following XAML:

    <?xml version="1.0" encoding="utf-8"?>
    <DataTemplate>
      <StackPanel Margin="3,3">
        <StackPanel.Resources>
          <ExtendedDataConverter p4:Key="ExtendedDataConverter" xmlns:p4="http://schemas.microsoft.com/winfx/2006/xaml" xmlns="http://schemas.telerik.com/2008/xaml/presentation" />
        </StackPanel.Resources>
        <TextBlock FontWeight="Bold" FontFamily="Trebuchet MS" FontSize="12" Text="{Binding Converter={StaticResource ExtendedDataConverter},ConverterParameter='value'}" />
        <ItemsControl ItemsSource="{Binding Converter={StaticResource ExtendedDataConverter},ConverterParameter='values'}">
          <ItemsControl.ItemTemplate>
            <DataTemplate>
              <TextBlock FontWeight="Bold" FontFamily="Trebuchet MS" FontSize="12" Text="{Binding}" />
            </DataTemplate>
          </ItemsControl.ItemTemplate>
        </ItemsControl>
      </StackPanel>
    </DataTemplate>


    Compare with the following test XAML which works fine in a WPF test app:

    <ItemsControl Name="listTest1" ItemsSource="{Binding}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding}" FontWeight="Bold" />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

    In my test app, I can even remove the ItemTemplate code and it works fine since, "By default, this will create a Vertical StackPanel, then it will loop
    through each item in MyCollection, add it to a TextBlock, then add that
    TextBox to the StackPanel."
     (http://rachel53461.wordpress.com/2011/09/17/wpf-itemscontrol-example/).

    I hope to eventually move the mess out to a separate xaml and merge it into the resource dictionary, once i figure out how...
  5. Answer
    Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 18 Aug 2014 Link to this post

    Hello Jason,

    We highly encourage you to create all resources such as templates / styles / custom styles / views only in XAML stored in XAML resource files or view files. This way it is much easier to maintain these resources, fix issues in them, reuse them, etc. Actually, the code in xaml is also more compact than the same code created in C# / VB.
    As for the particular binding scenario, is it possible for you to isolate it in a sample runnable project which is similar to your real world scenario and send it to us in a new support thread. This way we would be better able to investigate it, advice you more precisely and you will also take advantage from the 24 hours support response time which is guaranteed. Thank you in advance for your cooperation.

    Regards,
    Petar Mladenov
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  6. Jason D
    Jason D avatar
    72 posts
    Member since:
    Jan 2011

    Posted 18 Aug 2014 in reply to Petar Mladenov Link to this post

    I created a new support ticket with a sample. Thanks.
  7. Jason D
    Jason D avatar
    72 posts
    Member since:
    Jan 2011

    Posted 19 Aug 2014 Link to this post

    Helps if one registers the property before trying to set its value. Thanks to support for the quick turnaround.

    loPropertySet.RegisterProperty("values", "Value", GetType(List(Of String)), Nothing)
Back to Top
UI for WPF is Visual Studio 2017 Ready