3

I'm very new to WPF and I'm having difficulties binding a request object with nested objects which was derived from a WSDL to a XAML textbox. Programmatically I was able to bind to a textbox but I would like to understand the syntax needed to bind via XAML. Once I have some direction It'll make it much easier to research a full solution. Thanks

The ResultSet and Message Object will always be [0].

Code

MainWindow()
{
    InitializeComponent();
    GetMarketingMessagesResponse request = new GetMarketingMessagesResponse();
    request = (GetMarketingMessagesResponse)XMLSerializerHelper.Load(request, @"C:\SSAResponse.xml");
    DataContext = request;
    Binding bind = new Binding();
    bind.Source = request.ResultSet[0].Message[0];
    bind.Path = new PropertyPath("SubjectName");
    this.txtbSubject.SetBinding(TextBox.TextProperty, bind);
 }

The return value in the Visual Studio Watch bind.Source = request.ResultSet[0].Message[0]; is bind.Source = {GetMarketingMessagesResponseResultSetMessage} which is the class name.

XAML

I'm looking for direction on how to bind to this class and the properties inside

<TextBox Name="txtbMessageDetails" HorizontalAlignment="Right" Margin="0,50.08,8,0"    TextWrapping="Wrap" Text="{Binding Source=ResultSet[0].Message[0], Path=SubjectName}" VerticalAlignment="Top" Height="87.96" Width="287.942"/>

2 Answers 2

1

Use a converter which will receive the request and extract the message.

<Window.Resources>
    <local:MessageExtractorConverter x:Key="messageExtractorConverter" />
</Window.Resources>


<TextBox Name="txtbMessageDetails" HorizontalAlignment="Right" Margin="0,50.08,8,0"    TextWrapping="Wrap" Text="{Binding Converter={StaticResource messageExtractorConverter}" VerticalAlignment="Top" Height="87.96" Width="287.942"/>

Converter implementation:

public class MessageExtractorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var val = value as GetMarketingMessagesResponse;
        if (val != null)
        {
            // You can modify this code to extract whatever you want...
            return val.ResultSet[0].Message[0];
        }
        else
        {
            return null;
        }
   }
Sign up to request clarification or add additional context in comments.

Comments

1

You already put the request object in your DataContext, making it the default Source for all bindings. So, instead of specifying another Source (which would just override the DataContext), you use the binding's Path to make your way from the DataContext to the property you need:

<TextBox Name="txtbMessageDetails" Text="{Binding Path=ResultSet[0].Message[0].SubjectName}" />

Here is an article explaining how the DataContext works, and how it is "inherited" from control to control in your Window: http://www.codeproject.com/Articles/321899/DataContext-in-WPF

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.