2016-02-22 30 views
0

用戶控件數據綁定與這個簡單的代碼檢索值

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-我至少需要找到一種方法來從用戶界面和項目列表中刪除此控件。 2我喜歡在我的控制中訪問用戶名,並將其設置爲var 任何建議?

回答

0

解決方案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> 

所以,現在任何ButtonClick事件的處理程序是在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;    
    }