【问题标题】:Binding to a property in a collection which returns a sub collection绑定到返回子集合的集合中的属性
【发布时间】:2012-07-06 15:40:23
【问题描述】:

有没有办法将 ListBox 'ItemsSource' 绑定到 SeriaCollection XAxeLabels 或 YAxeLabels?

如果没有办法 - 我想知道为什么 - 解决方法是什么?

当前指定的绑定似乎没有这样做。

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>

【问题讨论】:

    标签: c# wpf xaml data-binding observablecollection


    【解决方案1】:

    将构造函数添加到您的 SeriaCollection 以创建 ObservableCollections:

    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 属性为您的班级分配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>
    

    两者都会显示您的内容。

    【讨论】:

      【解决方案2】:

      尝试在您的容器类上实现 INotifyPropertyChanged,并调用 OnPropertyChanged 来更新绑定 - 抱歉,现在不在我的办公桌前,所以不能更具体!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-08-27
        • 2022-01-10
        • 1970-01-01
        • 2012-04-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多