(某些)绑定和datatemplates(指定数据类型)不起作用

时间:2017-09-29 13:52:53

标签: c# wpf data-binding datatemplate

我有一个这样的XAML定义:

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

以下代码隐藏:

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;
    }
}

我还定义了以下类:

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; }
}

有什么问题?首先,在Expander.HeaderTemplate中定义的带有绑定的文本块不显示值。

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

第二个问题是datatemplates的数据类型(DataType =“local2:AnomalyComplex”和DataType =“local2:AnomalyStandard”)未自动使用。我应该使用DataTemplateSelectors吗?

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

您能否分析代码并告诉我哪里有问题,以及如何解决?提前谢谢。

2 个答案:

答案 0 :(得分:1)

  

首先,在Expander.HeaderTemplate中定义绑定的文本块不显示值。

Header属性绑定到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>

答案 1 :(得分:0)

如果DataTemplates具有指定的DataTypes,则应在花括号内指定,例如:

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