locked
Setting design time data for a binding that uses a value converter

    Question

  • Hi,

    I am trying to set up sample data to be used while I design a UI. I have successfully achieved this by using the tools in blend to create sample data based on my ViewModel.

    For simple bindings this has worked perfectly, but I can not figure out how to set up the sample data to display when a value converter is used.

    The bindings (even with converters) all work perfectly at run time.

    I decided that copying my code here probably wouldn't help but if there is anything you need or want to see, let me know!

    Any help anyone could offer would be great and thanks in advance.

    Daniel

    Sunday, March 30, 2014 4:12 PM

All replies

  • Hi,

    You can set a converter on any binding. To customize the converter for each scenario, create a class and implement the IValueConverter interface. The following example shows how to implement IValueConverter:

    // Custom class implements the IValueConverter interface.
    public class DateToStringConverter : IValueConverter
    {
    
        #region IValueConverter Members
    
        // Define the Convert method to change a DateTime object to 
        // a month string.
        public object Convert(object value, Type targetType, 
            object parameter, string language)
        {
            // value is the data from the source object.
            DateTime thisdate = (DateTime)value;
            int monthnum = thisdate.Month;
            string month;
            switch (monthnum)
            {
                case 1:
                    month = "January";
                    break;
                case 2:
                    month = "February";
                    break;
                default:
                    month = "Month not found";
                    break;
            }
            // Return the value to pass to the target.
            return month;
    
        }
    
        // ConvertBack is not implemented for a OneWay binding.
        public object ConvertBack(object value, Type targetType, 
            object parameter, string language)
        {
            throw new NotImplementedException();
        }
    
        #endregion
    }
    
    
    

    The following example shows how to set the Converter parameter:

    <UserControl.Resources>
      <local:DateToStringConverter x:Key="Converter1"/>
    </UserControl.Resources>
    
    ...
    
    <TextBlock Grid.Column="0" 
      Text="{Binding Month, Converter={StaticResource Converter1}}"/>
    
    

    For more information you can refer to the link topic of Data conversions below:

    http://msdn.microsoft.com/en-us/library/windows/apps/hh758320.aspx

    And see the links below:

    http://blogs.msdn.com/b/africaapps/archive/2013/06/05/windows-8-getting-stringformat-functionality.aspx

    http://blogs.msdn.com/b/mim/archive/2013/03/11/tips-winrt-converter-parameter-binding.aspx

    And refer to XAML data binding sample

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, March 31, 2014 2:06 AM
  • Thanks for your reply.

    However, i already have a successful data bind with a converter.

    the problem i am having is displaying the sample data at design time. Ie - while looking at the ui in blend...

    Monday, March 31, 2014 12:06 PM
  • Hi,

     You can also attempt to use d:DesignData, see some links below:

    http://blogs.msdn.com/b/wpfsldesigner/archive/2010/06/30/sample-data-in-the-wpf-and-silverlight-designer.aspx

    http://msdn.microsoft.com/en-us/library/ff602279(v=VS.95).aspx

    And refer to the thread:

    http://stackoverflow.com/questions/10965853/how-to-have-designtime-data-in-winrt-xaml

    Best Wishes!

     


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Wednesday, April 2, 2014 2:52 AM
  • Thanks again for your reply but this is still not answering my question...

    I have already used d:DesignData successfully to generate design time data for my application - that works fine.

    The problem I am having is that the design time data is not displaying for the bindings that are using a value converter.

    I am starting to wonder whether there is actually even a way of displaying design time data that relies on a converter?

    My internet searches have not found anything yet...

    Wednesday, April 2, 2014 10:58 AM
  • Hi,

    Please refer to the link below:

    http://www.codeproject.com/Articles/753250/Reminder-simple-Windows-Store-App

    And you can download the sample:

    Download Memo_01042014.zip

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, April 4, 2014 6:41 AM