zvi g

zvi g

  • NA
  • 10
  • 643

wpf binding not work on custom list object

Jan 21 2017 4:33 PM

in THIS code , binding not work:

 
  1. <Window x:Class="WpfApplication6.Window2"  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         xmlns:l="clr-namespace:WpfApplication6"  
  5.         Title="Window2" Height="300" Width="300">  
  6.     <Grid>  
  7.         <StackPanel>              
  8.             <Button Height="50" Width="100" Click="Button_Click_1">  
  9.                 <l:Item val="{Binding ElementName=aaa,Path=Text,Mode=TwoWay}" /> <!--work!!-->  
  10.             </Button>  
  11.             <Button Height="50" Margin="30" Click="Button_Click">  
  12.                 <l:ItemList>  
  13.                     <l:ItemList.MyList>  
  14.                         <l:Item val="{Binding ElementName=aaa, Path=Text, Mode=TwoWay}" /><!--not work!!-->  
  15.                         <l:Item val="{Binding ElementName=bbb, Path=Text, Mode=TwoWay}" /><!--not work!!-->  
  16.                     </l:ItemList.MyList>  
  17.                 </l:ItemList>                  
  18.             </Button>  
  19.             <TextBlock Text="aaa" Name="aaa" />  
  20.             <TextBlock Text="bbb" Name="bbb"/>  
  21.         </StackPanel>          
  22.     </Grid>  
  23. </Window> 
 c#:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Windows;  
  6. using System.Windows.Controls;  
  7. using System.Windows.Data;  
  8. using System.Windows.Documents;  
  9. using System.Windows.Input;  
  10. using System.Windows.Media;  
  11. using System.Windows.Media.Imaging;  
  12. using System.Windows.Shapes;  
  13. using System.Collections.ObjectModel;  
  14. using System.ComponentModel;  
  15.   
  16. namespace WpfApplication6  
  17. {  
  18.     /// <summary>  
  19.     /// Interaction logic for Window2.xaml  
  20.     /// </summary>  
  21.     public partial class Window2 : Window  
  22.     {  
  23.         public Window2()  
  24.         {  
  25.             InitializeComponent();              
  26.         }  
  27.   
  28.         private void Button_Click(object sender, RoutedEventArgs e)  
  29.         {           
  30.             ItemList il = (((Button)sender).Content) as ItemList;                          
  31.             MessageBox.Show(il.MyList[0].val);  //not work!  
  32.         }  
  33.   
  34.         private void Button_Click_1(object sender, RoutedEventArgs e)  
  35.         {              
  36.             Item i = (((Button)sender).Content) as Item;  
  37.             MessageBox.Show(i.val.ToString()); //work!  
  38.         }  
  39.   
  40.          
  41.     }  
  42.       
  43.     public class ItemList : Control  
  44.     {                       
  45.         public ObservableCollection<Item> MyList  
  46.         {  
  47.             get { return (ObservableCollection<Item>)GetValue(MyListProperty); }  
  48.             set { SetValue(MyListProperty, value); }  
  49.         }          
  50.         public static readonly DependencyProperty MyListProperty =  
  51.             DependencyProperty.Register("MyList"typeof(ObservableCollection<Item>), typeof(ItemList), new UIPropertyMetadata(new ObservableCollection<Item>()));         
  52.     }  
  53.   
  54.     public class Item : FrameworkElement  
  55.     {         
  56.         public string val  
  57.         {  
  58.             get { return (string)GetValue(valProperty); }  
  59.             set { SetValue(valProperty, value); }  
  60.         }          
  61.         public static readonly DependencyProperty valProperty =  
  62.             DependencyProperty.Register("val"typeof(string), typeof(Item), new UIPropertyMetadata(null,onChange));  
  63.   
  64.         private static void onChange(DependencyObject d, DependencyPropertyChangedEventArgs e)  
  65.         {  
  66.         }  
  67.     }  
  68.   

 

error msg:

System.Windows.Data Error: 4 :
Cannot find source for binding with reference 'ElementName=aaa'.
BindingExpression:Path=Text; DataItem=null; target element is 'Item' (Name=''); target property is 'val' (type 'String')

Does anyone know why it does not work?

Thanks