UserControl数据绑定检索值

时间:2016-02-22 10:11:02

标签: c# wpf xaml binding user-controls

使用这个简单的代码:

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:

<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:

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一切都会正常。 但!我应该如何在我的usercontrole代码中检索我的变量值? 看看我在UC中发表评论的地方。 1-我至少需要找到一种方法从UI和项目列表中删除此控件? 2 - 我想在我的控件中访问用户名并将其设置为var 有什么建议吗?

1 个答案:

答案 0 :(得分:0)

解决方案1:

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

<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:

更优雅的解决方案:

Style

中创建此MainWindow
<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;            
    }