WPF对象的绑定

时间:2012-07-02 22:14:14

标签: wpf listview binding taskbar

我有一个使用WPF开发的任务栏应用程序,我无法弄清楚如何将我的Object对象集合绑定到TrayPopUp。

这是我的集合类。它是在启动时创建的,并通过反序列化xmlfile(使用fillCollectionFromXml-method)填充对象:

public class OrdreCollection
{
    // Fields
    private ObservableCollection<Ordre> _ordreList = new ObservableCollection<Ordre>();


    // Properties
    public ObservableCollection<Ordre> OrdreList
    {
        get
        {
            if (_ordreList == null)
            {
                return new ObservableCollection<Ordre>();
            }
            return _ordreList;
        }

        set
        {
            _ordreList = value;
        }
    }


    //Methods

    public void fillCollectionFromXml(String filepath)
    {            
        XmlSerializer xs = new XmlSerializer(typeof(ObservableCollection<Ordre>));
        using (StreamReader rd = new StreamReader(filepath))
        {
            OrdreList = xs.Deserialize(rd) as ObservableCollection<Ordre>;
        }
    }

    public void createXmlFromCollection(OrdreCollection oCol, String filepath)
    {
        XmlSerializer xs = new XmlSerializer(typeof(ObservableCollection<Ordre>));
        using (StreamWriter wr = new StreamWriter(filepath))
        {
            xs.Serialize(wr, OrdreList);
        }
    }

    //public OrdreCollection getOrdrer(String filepath)
    //{
    //    OrdreCollection Ordrer = new OrdreCollection();
    //    Ordrer.fillCollectionFromXml("@../../Data/Ordrer.xml");

    //    return Ordrer;
    //}
}
}

基类:

[Serializable]
public class Ordre : INotifyPropertyChanged
{
    // INotifyPropertyChanged Member
    public event PropertyChangedEventHandler PropertyChanged;
    void Notify(string propName) {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }

    // Fields
    private DateTime ordreDato = DateTime.Today;
    private int ordreReference = 561978;
    private String bestiltAf;
    private List<OrdreItem> ordreItem;

    //Constructors

    public Ordre() { }

    public Ordre(DateTime ordredato, int ordrereference, String bestiltAf, List<OrdreItem> ordreItem)
    {
        this.Ordredato = ordredato;
        this.Ordrereference = ordrereference;
        this.BestiltAf = bestiltAf;
        this.OrdreItem = ordreItem;
    }


    //Properties

    public DateTime Ordredato
    {
        get { return ordreDato; }
        set { 
            ordreDato = value;
            Notify("OrdreDato");
        }
    }

    public int Ordrereference
    {
        get { return ordreReference; }
        set { 
            ordreReference = value;
            Notify("Ordrereference");
        }
    }

    public String BestiltAf
    {
        get { return bestiltAf; }
        set { 
            bestiltAf = value;
            Notify("BestiltAf");
        }
    }

    public List<OrdreItem> OrdreItem
    {
        get { return ordreItem; }
        set {
            ordreItem = value;
            Notify("OrdreItem");
        }
    }


    //Methods

    public void addItem(OrdreItem item)
    {
        OrdreItem.Add(item);
    }
}

[Serializable]
public class OrdreItem : INotifyPropertyChanged
{
    // INotifyPropertyChanged Member
    public event PropertyChangedEventHandler PropertyChanged;
    void Notify(string propName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }

    // Fields
    private String produktKode;
    private String produktNavn;
    private DateTime kalibreringsDato;
    private DateTime leveringsDato;
    private int antal;

    // Constructors
    public OrdreItem() { }
    public OrdreItem(String produktKode, String produktNavn, DateTime kalibreringsDato, DateTime leveringsDato, int antal)
    {
        this.produktKode = produktKode;
        this.produktNavn = produktNavn;
        this.kalibreringsDato = kalibreringsDato;
        this.leveringsDato = leveringsDato;
        this.antal = antal;
    }

    //Properties
    public String ProduktKode
    {
        get { return produktKode; }
        set {
            produktKode = value;
            Notify("ProduktKode");
        }
    }

    public String ProduktNavn
    {
        get { return produktNavn; }
        set {
            produktNavn = value;
            Notify("ProduktNavn");
        }
    }

    public DateTime KalibreringsDato
    {
        get { return kalibreringsDato; }
        set { 
            kalibreringsDato = value;
            Notify("KalibreringsDato");
        }
    }

    public DateTime LeveringsDato
    {
        get { return leveringsDato; }
        set { 
            leveringsDato = value;
            Notify("LeveringsDato");
        }
    }

    public int Antal
    {
        get { return antal; }
        set {
            antal = value;
            Notify("Antal");
        }
    }
}

这是我的XAML:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:tb="clr-namespace:Hardcodet.Wpf.TaskbarNotification;assembly=Hardcodet.Wpf.TaskbarNotification"
                    xmlns:local="clr-namespace:WpfApplication1.Models"
                    x:Class="WpfApplication1.Resources"
                    x:ClassModifier="public"
                    >
    <!-- Globaly declared OrdreCollection. Contains a list of orders.-->
    <local:OrdreCollection x:Key="OrdreList"></local:OrdreCollection>

    <!-- Globally declared notify icon -->
    <tb:TaskbarIcon x:Key="MyNotifyIcon" 
                    IconSource="/Icons/ge256x256new.ico" 
                    ToolTipText="GE Ordre Management"
                    PopupActivation="LeftOrDoubleClick"
                    DataContext=" "
                    >
    <!--
    We can use arbitrary UI elements as ToolTips.
    Let's use a semi-transparent border.
    -->

        <!-- Set a simple popup  -->
        <tb:TaskbarIcon.TrayPopup>
            <Border
                Background="White"
                BorderBrush="Orange"
                BorderThickness="2"
                CornerRadius="4" Margin="0" Padding="10">
                <GroupBox Header="Bestillinger" Height="Auto" Name="BestillingGroupBox" Width="445" HorizontalAlignment="Left" VerticalAlignment="Top" Padding="10">
                    <Grid>
                        <!--<ListBox ItemsSource="{Binding}" DisplayMemberPath="ordreDato"/>-->
                        <ListView IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding OrdreList}">
                            <ListView.View>
                                <GridView>
                                    <GridViewColumn Header="Ordredato" DisplayMemberBinding="{Binding Path=Ordredato}" />
                                    <GridViewColumn Header="Navn" DisplayMemberBinding="{Binding Path=BestiltAf}" />
                                    <GridViewColumn Header="Ordrenummer" DisplayMemberBinding="{Binding Path=Ordrereference}" />
                                </GridView>
                            </ListView.View>
                        </ListView>
                    </Grid>
                    <!--<Grid>
                        <Grid.DataContext>
                            <XmlDataProvider Source="@../../Data/employees.xml" />
                        </Grid.DataContext>
                        <Grid.Resources>
                            <HierarchicalDataTemplate x:Key="OrdreTemplate" DataType="Ordre">
                                <Border Background="White" BorderBrush="Orange" BorderThickness="2" CornerRadius="4" Margin="0" Padding="10" Width="400">
                                <TextBlock>
                                <TextBlock FontWeight="bold">Antal: </TextBlock>
                                <TextBlock Text="{Binding XPath=Antal}" />
                                <TextBlock FontWeight="bold">Produkt: </TextBlock>
                                <TextBlock Text="{Binding XPath=Produkt}" />
                                <LineBreak></LineBreak>
                                <TextBlock FontWeight="bold">Dato:</TextBlock>
                                <TextBlock Text="{Binding XPath=Dato}" />
                                <LineBreak></LineBreak>
                                <TextBlock FontWeight="bold">Bestilt af:</TextBlock>
                                <TextBlock Text="{Binding XPath=BestiltAf}" />
                                <LineBreak></LineBreak>
                                <TextBlock FontWeight="bold">Leveringsdato:</TextBlock>
                                <TextBlock Text="{Binding XPath=TilLevering}" />
                                <LineBreak></LineBreak>
                                <TextBlock FontWeight="bold">Ordrenr.:</TextBlock>
                                <TextBlock Text="{Binding XPath=OrdreNr}" />                            
                            </TextBlock>
                                </Border>
                            </HierarchicalDataTemplate>
                        </Grid.Resources>

                        <ListBox                         
                        Name="treeView" 
                        ItemsSource="{Binding Path=.,XPath=/Ordrer/Ordre}"
                        ItemTemplate="{StaticResource OrdreTemplate}" Background="white"
                        BorderBrush="Black"
                        BorderThickness="1"
                        Padding="4" />
                    </Grid>-->
                </GroupBox>
            </Border>
        </tb:TaskbarIcon.TrayPopup>
    </tb:TaskbarIcon>
</ResourceDictionary>

问题在于TrayPopup。我想在listview / listBox中显示OrderCollecion中所有Orders的值。我希望我的问题是可以理解的,而且我的代码不是太乱。当我使用XmlDataProvider填充列表框但不能使用ObjectdataProvider或直接从集合中填充时,我可以使它工作。

1 个答案:

答案 0 :(得分:1)

找到我的问题的答案。不得不使用CollectionViewSource。

<CollectionViewSource 
          Source="{Binding Source={x:Static Application.Current}, Path=OrdreItems}"   
          x:Key="ordreDataView" />