绑定与模板绑定(WPF)

时间:2010-06-14 19:44:06

标签: wpf data-binding

我有一个WPF UI绑定到AwesomeClass的集合

现在,AwesomeClass有一组AwesomeParts对象。

如何以这样的方式制作我的UI(例如)
对于每个AwesomeClass实例,选项卡面板上都有一个Tab 然后对于每个令人敬畏的部分,在该选项卡上的列表框上有一个对象。

基本上:Awesomes-> Tabs

然后:Awesome.Awesomeparts-> Tabs.Listbox

2 个答案:

答案 0 :(得分:2)

以下是执行您要找的内容的代码:

public partial class TabWindow : Window
{
    public TabWindow()
    {
        InitializeComponent();

        List<AwesomeClass> items = new List<AwesomeClass>();

        for (int i = 0; i < 10; i++)
        {
            items.Add(new AwesomeClass());
        }

        AwesomeTabs.ItemsSource = items;

        Loaded += new RoutedEventHandler(TabWindow_Loaded);
    }

    // Method 1
    void TabWindow_Loaded(object sender, RoutedEventArgs e)
    {
        FindListBox(AwesomeTabs);
    }

    private void FindListBox(DependencyObject obj)
    {
        Int32 count = VisualTreeHelper.GetChildrenCount(obj);
        for (int i = 0; i < count; i++)
        {
            var child = VisualTreeHelper.GetChild(obj, i);
            if (child is ListBox)
            {
                (child as ListBox).SelectionChanged += new SelectionChangedEventHandler(ListBox_SelectionChanged);
            }
            else
            {
                FindListBox(child);
            }
        }
    }

    // Method 2
    private void ListBox_Loaded(object sender, RoutedEventArgs e)
    {
        (sender as ListBox).SelectionChanged += new SelectionChangedEventHandler(ListBox_SelectionChanged);
    }

    void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        try
        {
            MessageBox.Show(e.AddedItems[0].ToString());
        }
        catch (Exception)
        { }
    }
}

class AwesomeClass
{
    static Int32 count = 0;

    public Int32 Index { get; set; }

    public List<AwesomePart> Parts { get; protected set; }

    // Method 3 : Preferred
    private AwesomePart _selectedPart;
    public AwesomePart SelectedPart
    {
        get { return _selectedPart; }
        set
        {
            OnSelectionChanged(_selectedPart, value);
            _selectedPart = value;
        }
    }

    private void OnSelectionChanged(AwesomePart oldValue, AwesomePart newValue)
    {
        if (newValue != null) MessageBox.Show(newValue.ToString());
    }

    public AwesomeClass()
    {
        Index = ++count;

        Parts = new List<AwesomePart>();

        for (int i = 0; i < 10; i++)
        {
            Parts.Add(new AwesomePart());
        }
    }

    public override string ToString()
    {
        return "Tab #" + Index.ToString();
    }
}

class AwesomePart
{
    static Int32 count = 0;

    public Int32 Index { get; set; }

    public AwesomePart()
    {
        Index = ++count;
    }

    public override string ToString()
    {
        return "Part #" + Index.ToString();
    }
}

XAML:

<Grid>
    <TabControl Name="AwesomeTabs">
        <TabControl.ContentTemplate>
            <DataTemplate>
                <ListBox ItemsSource="{Binding Parts}" SelectedItem="{Binding SelectedPart}" Loaded="ListBox_Loaded"></ListBox>
            </DataTemplate>
        </TabControl.ContentTemplate>
    </TabControl>
</Grid>

答案 1 :(得分:0)

绑定列表&lt; AwesomeClass&gt;加入内容控制。每个“AwesomeClass”对象将被设置为带标题内容控件中每个“选项卡”的datacontext。

在每个“选项卡”上的内容控件中,绑定访问List&lt; AwesomePart&gt;的DataContext(AwesomeClass)属性。到您的列表框控件。

有意义吗?

干杯。