UserControl数据绑定检索值 [英] UserControl Data Binding retrieve value

查看:101
本文介绍了UserControl数据绑定检索值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下简单代码:

MainWindows:

MainWindows:

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:

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>

然后是一个简单的UserControle:

then a simple UserControle:

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

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-我想在控件中访问用户名并将其设置为var 有什么建议吗?

Pressing F5 everything will bind ok if you test. But! how I'm supposed to retrieve my variable value in my usercontrole code? see where I put comment in UC. 1- I need at least to find a way to remove this control from UI and from Items list?. 2-I'd like access username in my control and set it into a var any suggestion?

推荐答案

解决方案1:

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

Use Tag property of button like below:

<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}}}" />

事件处理程序:

Event handler:

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:

更优雅的解决方案:

More elegant solution:

在您的MainWindow中创建此Style:

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

因此,现在任何Button Click事件的处理程序都在MainWindow.xaml.cs中.

So now the Handler of any Button Click event is in the MainWindow.xaml.cs.

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

Then change handler definition like below:

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;            
    }

这篇关于UserControl数据绑定检索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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