0

ListBox 'ItemsSource' を SeriaCollection XAxeLabels または YAxeLabels にバインドする方法はありますか?

方法がない場合 - 理由を知りたいのですが - 回避策は何ですか?

現在指定されている Binding はそうしていないようです。

public class AxeLabel
{
    public string Text { get; set; }
    public Brush Foreground { get; set; }
    public double X { get; set; }
    public double Y { get; set; }
}    

public class AxeLabels : ObservableCollection<AxeLabel>
{}

public class Seria
{
    public string Name { get; set; }
    public Brush Color { get; set; }
    public double Thickness { get; set; }
    public PointCollection Points { get; set; }
}


public class SeriaCollection : ObservableCollection<Seria>
{
    public AxeLabels XaxeLabels { get; set; }
    public AxeLabels YaxeLabels { get; set; }
}


   <Window.Resources>
    <my:SeriaCollection x:Key="mySeria">
        <my:SeriaCollection.YaxeLabels>
            <my:AxeLabel Text="3" X="0" Y="0" Foreground="Black" />
            <my:AxeLabel Text="5" X="0" Y="-10" Foreground="Black" />
            <my:AxeLabel Text="10" X="0" Y="-20" Foreground="Black" />
            <my:AxeLabel Text="20" X="0" Y="-30" Foreground="Black" />
        </my:SeriaCollection.YaxeLabels>
        <my:SeriaCollection.XaxeLabels>
            <my:AxeLabel Text="Jan-1-2013" X="0" Y="0" Foreground="Black" />
            <my:AxeLabel Text="Jan-9-2013" X="20" Y="0" Foreground="Black" />
            <my:AxeLabel Text="Jan-18-2013" X="40" Y="0" Foreground="Black" />
            <my:AxeLabel Text="Jan-27-2013" X="60" Y="0" Foreground="Black" />
        </my:SeriaCollection.XaxeLabels>
        <my:Seria Name="FirstSeria" Thickness="2">
            <my:Seria.Points>
                <PointCollection>
                    <Point X="10" Y="10" />
                    <Point X="50" Y="50" />
                    <Point X="100" Y="30" />
                </PointCollection>
            </my:Seria.Points>
            <my:Seria.Color>
                <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
                    <GradientStop Color="#FF8400FF" Offset="0" />
                    <GradientStop Color="#FF5EFF00" Offset="1" />
                </LinearGradientBrush>
            </my:Seria.Color>
        </my:Seria>
        <my:Seria Name="SecondSeria" Color="Blue" Thickness="6">
            <my:Seria.Points>
                <PointCollection>
                    <Point X="10" Y="60" />
                    <Point X="50" Y="30" />
                    <Point X="100" Y="20" />
                </PointCollection>
            </my:Seria.Points>
        </my:Seria>
    </my:SeriaCollection>
</Window.Resources>
<Grid>
    <ListBox ItemsSource="{Binding Source={StaticResource mySeria}, Path=YaxeLabels}" />
</Grid>
4

2 に答える 2

1

sSeriaCollectionを作成するために、コンストラクターをに追加します。ObservableCollection

public class SeriaCollection : ObservableCollection<Seria>
{
    public AxeLabels XaxeLabels { get; set; }
    public AxeLabels YaxeLabels { get; set; }

    public SeriaCollection()
    {
        XaxeLabels = new AxeLabels();
        YaxeLabels = new AxeLabels();
    }
}

を使用しますListBox.ItemTemplate

<ListBox ItemsSource="{Binding Source={StaticResource mySeria}, Path=YaxeLabels}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Text}" Width="100" />
                <TextBlock Text="{Binding X}" Width="50" />
                <TextBlock Text="{Binding Y}" Width="50" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

または、DataTypeプロパティを使用してクラスに a を割り当てDataTemplateます。

<Window.Resources>
    <DataTemplate DataType="{x:Type my:AxeLabel}">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding Text}" Width="100" />
            <TextBlock Text="{Binding X}" Width="50" />
            <TextBlock Text="{Binding Y}" Width="50" />
        </StackPanel>
    </DataTemplate>
</Window.Resources>

どちらもあなたのコンテンツを表示します。

于 2012-07-06T17:38:53.717 に答える
0

コンテナ クラスに INotifyPropertyChanged を実装し、OnPropertyChanged を呼び出してバインディングを更新してみてください。申し訳ありませんが、今は私のデスクにいないので、具体的にはできません。

于 2012-07-06T17:09:04.860 に答える