加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 从绑定用户控件访问Datacontext

发布时间:2020-12-15 20:58:31 所属栏目:百科 来源:网络整理
导读:我从一个observablecollection构建动态usercontrol cs代码: public static ObservableCollectionModel.Model.ControleData ListControleMachine = new ObservableCollectionModel.Model.ControleData(); public Genkai(string Autorisation) { InitializeCo
我从一个observablecollection构建动态usercontrol

cs代码:

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>

所以在我看来,我有一个类型为“local:ControlMachineII”的用户控件,与Model.Model.ControleData()绑定,但我如何从usercontrole c#代码访问ControleData?

例如,我想删除带有关闭按钮的usercontrole本身,我至少需要访问ControleData.ComputerName值然后从Mainform.ListControleMachine中删除它.

无法找到实现此目的的最佳实践,并在usercontrole代码中使用我的数据.

我认为删除按钮代码是这样的(具有硬编码值)

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

解决方法

我看到你今天发布了相同的 question以及更多数据.我将使用该数据提供解决方案.

解决方案1:

使用按钮的Tag属性如下:

<Button Content="Close this UC" HorizontalAlignment="Left" Margin="414,22,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中创建此样式:

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

So now the Handler of any Button Click event in any MainWindow's descendant Button Control is in the MainWindow.xaml.cs.

然后将处理程序方法放在MainWindow.xaml.cs中并更改处理程序,如下所示:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读