buks
buks

Reputation: 435

(some) bindings and datatemplates (with datatype specified) not working

I have a such XAML defined:

<UserControl x:Class="auditsListTest.AuditsList"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:auditsListTest"
         xmlns:local2="clr-namespace:auditsTest"

             mc:Ignorable="d"
             d:DesignHeight="300"
             d:DesignWidth="300">
    <UserControl.Resources>
        <DataTemplate  DataType="local2:AnomalyComplex">
            <DockPanel>
                <DockPanel DockPanel.Dock="Top">
                    <TextBlock DockPanel.Dock="Left"
                        Text="{Binding Value.Description}" />
                    <ComboBox DockPanel.Dock="Right">
                        <ComboBoxItem Content="Fix" />
                        <ComboBoxItem Content="Fix 2" />
                    </ComboBox>
                    <Image  DockPanel.Dock="Right"/>
                    <Button DockPanel.Dock="Right">
                        <TextBlock Text="Allow" />
                    </Button>
                </DockPanel>
                <ListBox ItemsSource="{Binding Value.GroupDescriptions}">
                    <ListBoxItem>
                        <DataTemplate>
                            <Expander Header="{Binding Key}" >
                                <DataTemplate>
                                    <ListBox ItemsSource="{Binding Value}" />
                                </DataTemplate>
                            </Expander>
                        </DataTemplate>
                    </ListBoxItem>
                </ListBox>
            </DockPanel>
        </DataTemplate>
        <DataTemplate DataType="local2:AnomalyStandard">
            <DockPanel DockPanel.Dock="Top">
                <TextBlock DockPanel.Dock="Left"
                     Text="{Binding Value.Description}" />
                <ComboBox DockPanel.Dock="Right">
                    <ComboBoxItem Content="Fix" />
                    <ComboBoxItem Content="Fix 2" />
                </ComboBox>
                <Image  DockPanel.Dock="Right" />
                <Button DockPanel.Dock="Right">
                    <TextBlock Text="Allow" />
                </Button>
                <TextBlock DockPanel.Dock="Bottom"
                   Text="{Binding Value.DescriptionDetailed}" />
            </DockPanel>
        </DataTemplate>
    </UserControl.Resources>
    <DockPanel Background="#FFA6FDE9">
        <TextBlock DockPanel.Dock="Top">Kontrolka audits</TextBlock>
        <ItemsControl  HorizontalContentAlignment="Stretch"
               x:Name="Tree"
               DockPanel.Dock="Top"
               ItemsSource="{Binding Anomalies}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Expander Name="expander"
                    Loaded="expander_Loaded"  IsExpanded="True"  >
                        <Expander.HeaderTemplate>
                            <DataTemplate>
                                <DockPanel LastChildFill="True"
                           HorizontalAlignment="Stretch">
                                    <TextBlock DockPanel.Dock="Left"
                             Text="{Binding  Key}" />
                                    <TextBlock DockPanel.Dock="Right"
                             HorizontalAlignment="Right">
                    <Run Text="Issues " />
                    <Run Text="{Binding Value.Count}" />
                                    </TextBlock>
                                </DockPanel>
                            </DataTemplate>

                        </Expander.HeaderTemplate>
                        <ContentControl>
                            <ItemsControl ItemsSource="{Binding Value}" />
                        </ContentControl>
                    </Expander>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </DockPanel>
</UserControl>    

with below code-behind:

public partial class AuditsList : UserControl
{
    public List<Anomaly> anomalies { get; set; }
    public Lookup<string, Anomaly> groupedAnomalies { get; set; }
    public List<KeyValuePair<string, List<Anomaly>>> groupedAnomalies2 { get; set; }

    public AuditsList()
    {
        groupedAnomalies2 = ReturnData();

        DataContext = new
        {
            Anomalies = groupedAnomalies2
        };
        InitializeComponent();
    }

    private List<KeyValuePair<string, List<Anomaly>>> ReturnData()
    {
        List<KeyValuePair<string, List<Anomaly>>> aaa = new List<KeyValuePair<string, List<Anomaly>>>();

        Slide sld1 = new Slide();
        sld1.Name = "slide1";
        sld1.SlideIndex = 1;

        Slide sld2 = new Slide();
        sld1.Name = "slide2";
        sld1.SlideIndex = 2;

        Slide sld3 = new Slide();
        sld1.Name = "slide3";
        sld1.SlideIndex = 3;

        Audit au1 = new Audit();
        au1.Description = "desc1";
        au1.Name = "audit1";
        au1.Priority = 1;

        Audit au2 = new Audit();
        au2.Description = "desc2";
        au2.Name = "audit2";
        au2.Priority = 2;


        List<string> descs1 = new List<string>();
        descs1.Add("Wlazł kotek na płotek");
        descs1.Add("Ala ma kota");

        KeyValuePair<string, List<string>> kvp1 = new KeyValuePair<string, List<string>>("Polski", descs1);

        List<string> descs2 = new List<string>();
        descs2.Add("Good morning captain!");
        descs2.Add("Fly! Fly! Fly away!");

        KeyValuePair<string, List<string>> kvp2 = new KeyValuePair<string, List<string>>("English", descs2);

        List<string> descs3 = new List<string>();
        descs3.Add("keine scheise!");
        descs3.Add("spreche dreche");

        KeyValuePair<string, List<string>> kvp3 = new KeyValuePair<string, List<string>>("Deutsch", descs1);


        AnomalyComplex ano1 = new AnomalyComplex();
        ano1.Audit = au1;
        ano1.Name = au1.Name;
        ano1.Description = au1.Name;
        ano1.Slide = sld1;
        ano1.GroupDescriptions = new List<KeyValuePair<string, List<string>>>();
        ano1.GroupDescriptions.Add(kvp1);
        ano1.GroupDescriptions.Add(kvp2);
        ano1.GroupDescriptions.Add(kvp3);



        AnomalyStandard ano2 = new AnomalyStandard();

        ano2.Audit = au2;
        ano2.Name = au2.Name;
        ano2.Slide = sld2;
        ano2.DescriptionDetailed = "trele morele";
        ano2.Description = au2.Name;


        AnomalyStandard ano3 = new AnomalyStandard();
        ano3.Audit = au2;
        ano3.Name = au2.Name;
        ano3.Slide = sld3;
        ano3.Description = au2.Name;
        ano3.DescriptionDetailed = "bomba trąba";

        anomalies = new List<Anomaly>();
        anomalies.Add(ano1);
        anomalies.Add(ano2);
        anomalies.Add(ano3);

        groupedAnomalies = (Lookup<string, Anomaly>)anomalies.OrderBy(c => c.Audit.Priority).ToLookup(c => c.Audit.Priority == 1 ? "Slide " + c.Slide.SlideIndex.ToString() : "Presentation");
        aaa = groupedAnomalies.Select(c => new KeyValuePair<string, List<Anomaly>>(c.Key, c.ToList())).ToList();

        return aaa;
    }
}

I have also defined below classes:

namespace auditsListTest
{
    public class Slide
    {
        public int SlideIndex { get; set; }
        public string Name { get; set; }
    }
}

public class Audit
{
    public string Name { get; set; }
    public string Description { get; set; }
    public int Priority { get; set; }
    public virtual List<Anomaly> PerformAudit(double progress)
    {
        return new List<Anomaly>();
    }
}
public class Anomaly
{
    public string Name { get; set; }
    public Audit Audit { get; set; }
    public string Description { get; set; }
    public bool Fixed { get; set; }
    public bool Allowed { get; set; }
    public string Container { get; set; }
    public Slide Slide { get; set; }

    public void Fix()
    {
        Fixed = true;
    }

    public void Allow()
    {
        Allowed = true;
    }

}
public class AnomalyComplex : Anomaly
{
    public List<KeyValuePair<string, List<string>>> GroupDescriptions { get; set; }
}
public class AnomalyStandard : Anomaly
{
    public string DescriptionDetailed { get; set; }
}

What is the problem? First, textblocks with binding defined in Expander.HeaderTemplate are not showing values.

<TextBlock DockPanel.Dock="Left"
                         Text="{Binding  Key}" />
<TextBlock DockPanel.Dock="Right"
                         HorizontalAlignment="Right">
                <Run Text="Issues " />
                <Run Text="{Binding Value.Count}" />

Second problem is with datatemplates with datatypes (DataType="local2:AnomalyComplex" and DataType="local2:AnomalyStandard") that are not automatically used. Should I use DataTemplateSelectors?

<ContentControl>
    <ItemsControl ItemsSource="{Binding Value}" />
</ContentControl>

Could you analyze code and let me know where is problem, and how to solve it? Thanks in advance.

Upvotes: 0

Views: 207

Answers (2)

buks
buks

Reputation: 435

In case of DataTemplates with specified DataTypes, it should be specified inside curly braces, for example:

<DataTemplate  DataType="{x:Type local2:AnomalyComplex}">
<DataTemplate  DataType="{x:Type local2:AnomalyStandard}">

Upvotes: 0

mm8
mm8

Reputation: 169200

First, textblocks with binding defined in Expander.HeaderTemplate are not showing values.

Bind the Header property to the KeyValuePair<string, List<Anomaly>>:

<Expander Name="expander" Header="{Binding}">
    <Expander.HeaderTemplate>
        <DataTemplate>
            <DockPanel LastChildFill="True" HorizontalAlignment="Stretch">
                <TextBlock DockPanel.Dock="Left" Text="{Binding  Key}" />
                <TextBlock DockPanel.Dock="Right" HorizontalAlignment="Right">
                            <Run Text="Issues " />
                            <Run Text="{Binding Value.Count, Mode=OneTime}" />
                </TextBlock>
            </DockPanel>
        </DataTemplate>
    </Expander.HeaderTemplate>
    ...
</Expander>

Upvotes: 1

Related Questions