用户控件数据绑定检索值

本文关键字:检索 数据绑定 控件 用户 | 更新日期: 2023-09-27 18:35:36

用这个简单的代码:

主窗口:

using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace itemcontrole_lesson
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
  public class TodoItem
        {
            public string Username { get; set; }
            public int Completion { get; set; }
        }

    public partial class MainWindow : Window
    {  
        List<TodoItem> items = new List<TodoItem>();
        public MainWindow()
        {
            InitializeComponent();
            items.Add(new TodoItem() { Username = "Eric", Completion = 45 });
            items.Add(new TodoItem() { Username = "Maxwell", Completion = 80 });
            items.Add(new TodoItem() { Username = "Sarah", Completion = 60 });
           icTodoList.ItemsSource = items;
        }
    }

Mainwindows XAML:

<Window x:Class="itemcontrole_lesson.MainWindow"
        xmlns:local="clr-namespace:itemcontrole_lesson"  
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ItemsControl x:Name="icTodoList">
            <ItemsControl.ItemTemplate>
                <DataTemplate DataType="{x:Type local:UserControl1}">
                    <local:UserControl1 />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</Window>

然后是一个简单的用户控件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace itemcontrole_lesson
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
            //String  CurentUserName = ????????
            //Int  Progress = ????????
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ///hows to remove the user from items in main windows??
        }
    }
}

UserControle XAML

<UserControl x:Class="itemcontrole_lesson.UserControl1"
             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="54.181" Width="399.331">
    <Grid Margin="0,0,-155,0">
        <Label Content="{Binding Username}" HorizontalAlignment="Left" Margin="23,23,0,0" VerticalAlignment="Top"/>
        <Button Content="Close this UC" HorizontalAlignment="Left" Margin="414,22,0,0" VerticalAlignment="Top" Width="119" Click="Button_Click"/>
        <ProgressBar HorizontalAlignment="Left" Value="{Binding Completion}" Height="10" Margin="204,23,0,0" VerticalAlignment="Top" Width="154"/>
    </Grid>
</UserControl>

按 F5 如果您测试,所有内容都将绑定正常。但!我应该如何在我的用户控件代码中检索我的变量值?看看我在UC中发表评论的地方。1-我至少需要找到一种方法从UI和项目列表中删除此控件?2-我想在我的控件中访问用户名并将其设置为变量有什么建议吗?

用户控件数据绑定检索值

解决方案 1:

使用按钮的标签属性,如下所示:

<Button Content="Close this UC" HorizontalAlignment="Left" Margin="414,22,0,0" 
            VerticalAlignment="Top" Width="119" Click="Button_Click" Tag="{Binding RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />

事件处理程序:

private void Button_Click(object sender, RoutedEventArgs e)
    {
         var button = sender as Button;
         List<object> list = (button.Tag as ItemsControl).ItemsSource.OfType<TodoItem>().ToList<object>();
        list.Remove(button.DataContext);
        (button.Tag as ItemsControl).ItemsSource = list;
    }

解决方案 2:

更优雅的解决方案:

在您的MainWindow中创建此Style

<Window.Resources>
    <Style TargetType="Button">
        <EventSetter Event="Click" Handler="Button_Click"/>
    </Style>
</Window.Resources>

所以现在任何Button Click事件的处理程序都在MainWindow.xaml.cs中。

然后更改handler定义,如下所示:

private void Button_Click(object sender, RoutedEventArgs e)
    {
        var button = sender as Button;
        items.Remove(button.DataContext as TodoItem);
        icTodoList.ItemsSource = null;
        icTodoList.ItemsSource = items;            
    }