在WPF中绑定用户控件

编程入门 行业动态 更新时间:2024-10-21 18:33:13
本文介绍了在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="schemas.microsoft/winfx/2006/xaml/presentation" xmlns:x="schemas.microsoft/winfx/2006/xaml" xmlns:mc="schemas.openxmlformats/markup-compatibility/2006" xmlns:d="schemas.microsoft/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中绑定用户控件

本文发布于:2023-11-08 07:11:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1568693.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   控件   用户   WPF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!