在WPF中绑定用户控件 [英] Binding a usercontrol in WPF

查看:82
本文介绍了在WPF中绑定用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我在C#中有一个WPF程序.在此程序中,我将对象集合绑定到tabControl:

Hi I have a WPF program in C#. In this program, I binded an object collection to tabControl:

<TabControl x:Name="tabControlMain" Margin="1,1,48,1" DataContext="{Binding}" Grid.RowSpan="2" Grid.ColumnSpan="2">
    <TabControl.ItemTemplate>
        <DataTemplate DataType="TabpageItem">
            <TextBlock Text="{Binding Title}">
                <TextBlock.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Remove" Click="MenuItem_Remove_Click">
                            <MenuItem.Icon>
                                <Image Width="20" Height="20"  Source="..\Resources\remove.png" Stretch="Fill" />
                            </MenuItem.Icon>
                        </MenuItem>
                        <MenuItem Header="Rename" >
                            <MenuItem.Icon>
                                <Image Height="20"  Source="..\Resources\rename.png" Stretch="UniformToFill" />
                            </MenuItem.Icon>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="Rename to:   " VerticalAlignment="Center"></TextBlock>
                                <TextBox Text="{Binding Title}" GotMouseCapture="TextBox_GotMouseCapture"></TextBox>
                            </StackPanel>
                        </MenuItem>
                    </ContextMenu >
                </TextBlock.ContextMenu>
            </TextBlock>
        </DataTemplate>
    </TabControl.ItemTemplate>
        <TabControl.ContentTemplate>
            <DataTemplate>
                        <myUserControl:UserControlTabpageContent></myUserControl:UserControlTabpageContent>
            </DataTemplate>
        </TabControl.ContentTemplate>
    <TabItem Header="tabItem1" Name="tabItem1" >
        <myUserControl:UserControlTabpageContent></myUserControl:UserControlTabpageContent>
    </TabItem>
</TabControl>



和代码:



and the code:

public MainWindow()
{
    InitializeComponent();
    //////////////////////////////////////////////////////////////////////////
    Load();
    tabControlMain.Items.Clear();
    tabControlMain.ItemsSource = totalInfoCollection.TabPageCollection;
}




每个选项卡中都有一个UserControl:




Inside each tab there is a UserControl:

<UserControl

             x:Class="InfoManager.Presentation_Layer.UserControlTabpageContent"

             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

             mc:Ignorable="d"

             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Grid Name="grid1">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Grid Name="grid2">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <TextBlock Name="textBlock1" Text="Select by tags:" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="10" />
                <TextBox Grid.Column="1" Text="{Binding SerachString,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"  Name="textBoxSelectionTags"  VerticalAlignment="Center" Margin="10" />
            </Grid>
            <ListView DataContext="{Binding}" ItemsSource="{Binding InfoItemSerachResult,UpdateSourceTrigger=PropertyChanged}"  Grid.Row="1" Name="listView1" Margin="10">
                <ListView.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Add" >
                            <MenuItem.Icon>
                                <Image Height="20"  Source="..\Resources\add.png" Stretch="UniformToFill" />
                            </MenuItem.Icon>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="Add:   " VerticalAlignment="Center"></TextBlock>
                                <TextBox Name="TextBox_AddNewInfoName"  GotMouseCapture="TextBox_GotMouseCapture" MinWidth="100"></TextBox>
                                <Button Name="Button_AddNewInfo" Click="Button_AddNewInfo_Click" Content="Add"></Button>
                            </StackPanel>
                        </MenuItem>
                    </ContextMenu >
                </ListView.ContextMenu>
            </ListView>
        </Grid>
    </Grid>
</UserControl>





在用户控件内部,我已经成功编写了一些代码来获取绑定到控件的对象:





Inside the user control I have successfully written some codes for obtaining the binded object to the controls:

private void Button_AddNewInfo_Click(object sender, RoutedEventArgs e)
{
    TabpageItem t=(((((sender as Button).Parent as StackPanel).Parent as MenuItem).Parent as ContextMenu).PlacementTarget as ListView).DataContext as TabpageItem;
    //t.
   // (sender as )
}



现在我的问题是在usercontrol构造函数内部,我不知道如何获取与usercontrol绑定的对象?



Now my problem is that inside the usercontrol constructor i dont know how to obtain the binded object to the usercontrol?

public partial class UserControlTabpageContent : UserControl
 {
     public UserControlTabpageContent()
     {
         InitializeComponent();
         listView1.Items.Clear();
        // listView1.ItemsSource = ??????;
     }

推荐答案

"this.DataContext as TabpageItem"应该给您绑定对象",但尚未在构造函数中设置DataContext.

必须先创建对象,然后才能进行任何绑定.

如果需要将listView1.ItemsSource绑定到的数据在TabpageItem类中,则应使用Binding,当设置DataContext时将进行绑定.

我不确定为什么要继续尝试在代码中重复XAML中已有的内容.也许使用其中一个?您已经在XAML中为listview的ItemsSource绑定了.而且您不需要DataContext ="{Binding}":)
"this.DataContext as TabpageItem" should give you the "binded object" but DataContext won''t be set yet in the constructor.

The object has to be created first before any binding occurs.

If the data you need to bind listView1.ItemsSource to is in the TabpageItem class, then you should use a Binding, which will bind when the DataContext is set.

I''m not sure why you keep trying to duplicate in code what you already have in XAML. Perhaps use one or the other? You already have a binding in XAML for the listview''s ItemsSource. And you don''t need DataContext="{Binding}" :)


这篇关于在WPF中绑定用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆