1

WPFで開発されたタスクバーアプリケーションがありますが、Objectbindingを使用してオブジェクトのコレクションを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にあります。これは、私のOrderCollecion内のすべての注文の値をlistview/listBoxに表示したいと思います。私の質問が理解でき、私のコードがあまり乱雑ではないことを願っています。リストボックスに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 に答える