从绑定的用户控件访问 Datacontext
Access Datacontext from binded user control
我已经从 ObservableCollection 构建了一个动态 UserControl,如下所示...
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>
但是如何从 C# 代码访问 DataContext?
例如,假设我想删除本身带有关闭按钮的 UserControl,我至少需要访问 ControleData.ComputerName 值,然后将其从 Mainform.ListControleMachine 中删除。
我找不到实现此目的的最佳实践并在 UserControl 代码中使用我的数据。
删除按钮代码我认为是这样的(具有硬编码值)
Genkai.ListControleMachine.Remove(Genkai.ListControleMachine.Where(X => X.ComputerName == "M57095").Single());
我看到您今天发布了相同的 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;
}
方案二:
更优雅的解法:
在您的 MainWindow
中创建此 Style
:
<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
.
然后将 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;
}
我终于发现我的 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);
}
我已经从 ObservableCollection 构建了一个动态 UserControl,如下所示...
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>
但是如何从 C# 代码访问 DataContext?
例如,假设我想删除本身带有关闭按钮的 UserControl,我至少需要访问 ControleData.ComputerName 值,然后将其从 Mainform.ListControleMachine 中删除。
我找不到实现此目的的最佳实践并在 UserControl 代码中使用我的数据。
删除按钮代码我认为是这样的(具有硬编码值)
Genkai.ListControleMachine.Remove(Genkai.ListControleMachine.Where(X => X.ComputerName == "M57095").Single());
我看到您今天发布了相同的 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;
}
方案二:
更优雅的解法:
在您的 MainWindow
中创建此 Style
:
<Window.Resources>
<Style TargetType="Button">
<EventSetter Event="Click" Handler="Button_Click"/>
</Style>
</Window.Resources>
So now the Handler of any
Button
Click
event in anyMainWindow's
descendantButton
Control is in theMainWindow.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;
}
我终于发现我的 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);
}