Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Binding nested objects

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"/>

like image 838
user1943959 Avatar asked Feb 28 '26 12:02

user1943959


1 Answers

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;
        }
   }
like image 62
Blachshma Avatar answered Mar 02 '26 01:03

Blachshma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!