1

我有一个使用 WPF 开发的任务栏应用程序,我只是不知道如何使用 Objectbinding 将我的对象集合绑定到 TrayPopUp。

这是我的收藏课。它是在启动时创建的,并通过反序列化 xml 文件(使用 fillCollectionFromXml 方法)填充对象:

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。我想在列表视图/列表框中显示我的 OrderCollecion 中所有订单的值。我希望我的问题是可以理解的,并且我的代码不会太乱。当我用 XmlDataProvider 填充列表框时,我可以让它工作,但不能用 ObjectdataProvider 或直接从集合中填充。

4

1 回答 1

1

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

<CollectionViewSource 
          Source="{Binding Source={x:Static Application.Current}, Path=OrdreItems}"   
          x:Key="ordreDataView" />
于 2012-07-03T22:37:40.880 回答