将控件与可观察集合绑定

时间:2015-04-26 02:23:59

标签: c# wpf xaml

在xaml中,如何在展开时使用网格在扩展器控件中显示ObservableCollection?

public class BoundPoco
(
    public string ExpanderText { set; get; }
    public ObservableCollection<DisplayDetail> { set; get; }
)

public class DisplayDetail
{
    public string FirstDisplayColumn { set; get; }
    public string SecondDisplayColumn; { set; get; }
}

1 个答案:

答案 0 :(得分:0)

您应该使用扩展器控件中的项控件来实现此目的。

首先将你的代码排序错误 - 你在“SecondDisplayColumn”上有一个迷路分号:

    If Something Then
        ' Starts the music.
        OLEPlayer.Action = 7
        ' Here, which line I should use to hide the MPlayer itself?
        ' OLEPlayer.Visible = False - hides just the controler, and not its class.
        ' There is no Visible property to the Class.
    Else
        ' Stops the music.
        oleAlarmSound.Action = 9
    End If

...主要的可观察集合属性上没有名字!

public string SecondDisplayColumn { set; get; }

然后,如果您没有使用依赖项属性或INotifyPropertyChanged - 创建您的XAML以显示如此数据(名称为“items”):

public ObservableCollection<DisplayDetail> DisplayDetails { set; get; }

然后,您可以填充可观察集合并在代码隐藏中设置项目源:

<Expander Name="myExpander" Background="Tan" 
      HorizontalAlignment="Left" Header="My Expander" 
      ExpandDirection="Down" IsExpanded="True" Width="Auto">
    <ItemsControl Name="items">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>

                    <TextBlock Grid.Column="0" Text="{Binding FirstDisplayColumn}"/>
                    <TextBlock Grid.Column="1" Text="{Binding SecondDisplayColumn}"/>
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Expander>