Access Datacontext from a bound user control

i builds dynamic user control from observable collectibles

Cs code:

 public  static ObservableCollection<Model.Model.ControleData> ListControleMachine = new ObservableCollection<Model.Model.ControleData>();



     public Genkai(string Autorisation)
            {

                InitializeComponent();

                DataContext = this;

               icTodoList.ItemsSource = ListControleMachine;
               Model.Model.ControleData v = new Model.Model.ControleData();
               v.ComputerName = "M57095";
               v.ImportSource = "LOAD";
               ListControleMachine.Add(v);
    }

Xaml

  <ItemsControl x:Name="icTodoList" ItemsSource="{Binding ListControleMachine}" >

                                        <ItemsControl.ItemTemplate>
                                            <DataTemplate DataType="{x:Type local:ControlMachineII}">
                                                <local:ControlMachineII  />

                                            </DataTemplate>
                                        </ItemsControl.ItemTemplate>
                                    </ItemsControl>

therefore, in my opinion, I have a usercontrole of type " local:ControlMachineII" associated with Model.Model.ControleData (), but how can I access ControleData from C # usercontrole code?

For example, let's say I want to remove usercontrole using the close button by itself, I need to at least access ControleData.ComputerName, and then remove it from Mainform.ListControleMachine.

cannot find the best practice to achieve this and play with my data in usercontrole code.

the delete button code looks the way I think (with a hard-coded value)

  Genkai.ListControleMachine.Remove(Genkai.ListControleMachine.Where(X => X.ComputerName == "M57095").Single());
+4
2

, , DataContext , , datacontext:

     public ControlMachineII()
            {
                InitializeComponent();
                DataContextChanged += new DependencyPropertyChangedEventHandler(ControlMachineII_DataContextChanged);


            }

   private void ControlMachineII_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            string compname = (this.DataContext as Model.Model.ControleData).ComputerName;
            Console.WriteLine("DataContext initialized computername :" +compname);
        }
0

, question . , .

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:

:

Style MainWindow:

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

, Button Click MainWindow's Button MainWindow.xaml.cs.

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

Source: https://habr.com/ru/post/1629372/


All Articles