Stef
Stef

Reputation: 623

TreeView Binding Issue

I'm trying to port my Forms code to WPF.

Currently I want to post a DNS Zonetransfer to a Treeview.

MainWindow.cs does something like:

Response axfr = new Response();
axfr = dig.DigIt();
DataContext = axfr.Answers;

A Response contains Answers, which contains ResourceRecords.

    public class Response
    {
            public List<AnswerRR> Answers { get; set; }

[...]

public class ResourceRecord
{
    public string NAME {get; set;}
}

public class AnswerRR : ResourceRecord
{

}

And using this XAML:

    <HierarchicalDataTemplate DataType="{x:Type Model:Response}"
                              ItemsSource="{Binding AnswerRR }">
        <TextBlock Text="{Binding Name}" ToolTip="{Binding Name}" />
    </HierarchicalDataTemplate>

However, I always get the full ResourceRecord as output (my.test.com. 600 IN A 1.2.3.4) instead the name only!

What am I doing wrong here.

Upvotes: 0

Views: 177

Answers (2)

Stipo
Stipo

Reputation: 4606

You have few mistakes in your data template and binding.

First you are defining data template for Response and in it you are binding ItemsSource to AnswerRR. Response class doesn't have an AnswerRR property, it should be Answers.

Also you are binding to Name property from Response data template. Response doesn't contain Name property. ResourceRecord contains NAME property, so you also need to define data template for ResourceRecord and bind to NAME property from it. Also take care about case sensitivity. Bindings are case-sensitive.

Here is a complete XAML for all DataTemplates:

<HierarchicalDataTemplate DataType="{x:Type Model:Response}" ItemsSource="{Binding Answers}">
</HierarchicalDataTemplate>

<HierarchicalDataTemplate DataType="{x:Type Model:ResourceRecord}" ItemsSource="{Binding TestResults}">
    <TextBlock Text="{Binding NAME}" ToolTip="{Binding NAME}"/>
</HierarchicalDataTemplate >

<DataTemplate DataType="{x:Type Model:DNSTestResult}">
    <TextBlock Text="{Binding resultValue}" ToolTip="{Binding resultValue}"/>
</DataTemplate>

DNSTestResult.resultValue must be a property, not field!

Upvotes: 1

Josh
Josh

Reputation: 2975

From what I'm seeing your binding is pointing incorrectly. It should be:

<TextBlock Text="{Binding NAME}" ToolTip="{Binding NAME}" />

Upvotes: 0

Related Questions