- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
关闭。这个问题需要更多 focused .它目前不接受答案。
想改进这个问题?更新问题,使其仅关注一个问题 editing this post .
5年前关闭。
Improve this question
我有一些 UserControls
ItemsControl
的内部.
每个UserControl
(在我的例子中称为 JobView
)有一个 ContextMenu
与 1 Item
('消除')。
现在当项目上UserControls ContextMenu
是 Clicked
我想从 ItemsControlItemCollection
中删除它.
为此,我需要获取 Item
, ContextMenu 被分配到。
目前我正在使用这个:
private void Item_Click(object sender, RoutedEventArgs e)
{
MenuItem item = (MenuItem)sender;
JobView view = null;
FrameworkElement currentObject = item;
while(1 == 1)
{
currentObject = currentObject.Parent as FrameworkElement;
if(currentObject.GetType() == typeof(System.Windows.Controls.Primitives.Popup))
{
view = (currentObject as System.Windows.Controls.Primitives.Popup).PlacementTarget as JobView;
break;
}
}
//Remove from ObservableCollection<JobView>:
JobViews.Remove(view);
}
JobView
使用 sender
目的sender
在这种情况下完全错误?
最佳答案
将 ItemsControl 的 ItemsSource 绑定(bind)或设置为 UIElements 或 View 的 ObservableCollection 是错误的。至少如果您关心 MVVM 设计模式,这是推荐用于所有基于 XAML 的应用程序的模式。
您应该创建一个代表 JobView 状态的类并绑定(bind)到此类对象的 ObservableCollection,例如:
public class Job
{
}
public class JobViewModel
{
public ObservableCollection<Job> Jobs { get; } = new ObservableCollection<Job>()
{
new Job(),
new Job(),
new Job()
};
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new JobViewModel();
}
}
<ItemsControl ItemsSource="{Binding Jobs}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<local:JobView />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
public class JobViewModel
{
public JobViewModel()
{
RemoveCommand = new DelegateCommand<object>(argument =>
{
Jobs.Remove(argument as Job);
});
}
public ObservableCollection<Job> Jobs { get; } = new ObservableCollection<Job>()
{
new Job(),
new Job(),
new Job()
};
public DelegateCommand<object> RemoveCommand { get; }
}
public partial class JobView : UserControl
{
public JobView()
{
InitializeComponent();
}
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(JobView));
public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
}
<UserControl x:Class="WpfApplication1.JobView"
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"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
x:Name="uc">
<UserControl.ContextMenu>
<ContextMenu>
<MenuItem Header="Remove" Command="{Binding PlacementTarget.Command, RelativeSource={RelativeSource AncestorType=ContextMenu}}"
CommandParameter="{Binding}"/>
</ContextMenu>
</UserControl.ContextMenu>
<Grid>
<TextBlock>job view...</TextBlock>
</Grid>
</UserControl>
<ItemsControl ItemsSource="{Binding Jobs}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<local:JobView Command="{Binding DataContext.RemoveCommand, RelativeSource={RelativeSource AncestorType=ItemsControl}}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
关于c# - 从 ItemsControl 中删除项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41428920/
我正在使用 M-V-VM 并在我的 ViewModel 上有一个名为“EntitySelectedCommand”的命令。 我试图让 ItemsControl 中的所有项目来触发这个命令,但是它不起作
在我的 View 中有一个 ItemsControl,它绑定(bind)到来自 ViewModel 的 ObservableCollection。集合被填充,然后引发从 VM 到 View 的事件(想
我最近遇到了一个虚拟化问题,我将其缩小到以下代码。 虚拟化在以下代码段中不起作用的原因是因为 child 没有特定的高度。所以我的猜测是它会永远扩展并且虚拟化会中断。 给 child 一个特定的高度解
我是 WPF 的新手。我一直在尝试执行以下操作: 以下是我的数据结构: public class TokenItems {public string items {get;set;} public i
我正在尝试从 WPF MVVM 教程中扩展应用程序作为练习。对于我在这里面临的这个特定问题,我在网上没有找到解决方案。 我有一个名为“StudentsToAdd”的带有 ObservableColle
我正在使用带有 MVVM 模式的 Wpf,所以在 Xamel 中,我有一个 itemControl 持有另一个 itemcontrol,每个 itemcontrol 绑定(bind)来自不同的 Obs
标题可能听起来令人费解,但请耐心等待。 我有包含住户的房间: public class Room { public string Name { get; set; } public L
我需要能够从子 ItemsControll 数据模板内部绑定(bind)到父 ItemsControl 的属性:
这让我发疯。也许我的数据设计有误,但我正在尝试从父 ItemsControl 绑定(bind)到事件项目。 每个区域都有一种颜色,以便在屏幕上轻松识别。我没有为每个座位赋予颜色属性,而是在 Area
我的问题与此非常相似:( Binding events to buttons in an ItemsControl ) 但我没有在那里找到解决方案。我有一个 ItemsControl,在它的 Data
考虑以下 XAML
在我的主视图中,我有一个绑定(bind)到对象集合的 ItemsControl: ActivationLevelTemplate 只是另一个 View : 在这个 View 中有一
我正在使用 ItemsControl,其中 ItemsPanel 设置为 Canvas(有关更多背景信息,请参阅 this 问题)。ItemsControl 正在按我想要的方式执行,并且通过将子元素放
我有一个 ScrollViewer,里面有一个 ItemsControl。 ItemsControl 的 ItemSource 绑定(bind)到 ObservableCollection。 问题是它
我有一个带有 itemscontrol 的 WPF MVVM 应用程序。水平子项的数量受 itemscontrol 宽度的影响。只有一件事我没有解决。我试图以始终居中的方式对齐子元素。我已经用油漆制作
我有以下 XAML: 当我将数据拖到此面板上时,鼠标光标显示允许在所有子项上放置,但在任何空白区域上,光标显示禁止放置。如果我设置 AllowDro
我有一些数据要显示在 FlowDocument 中.这基本上是一个以友好方式解释数据的 View ,包括节标题、文本段落等,我将在 FlowDocumentScrollViewer 中显示这些 Vie
我有一个 ItemsControl包含我想虚拟化的数据列表,但是 VirtualizingStackPanel.IsVirtualizing="True"似乎不适用于 ItemsControl . 真
我想写一个 ItemsControl 派生的自定义控件。这部分是出于需要,部分是作为学习练习 - 请不要建议 I Style、DataTemplate、ControlTemplate 和 ListBo
我想使用ItemsControl显示重要的项目列表。 我使用ItemsControl的原因是,我正在处理的应用程序中的DataTemplate要复杂得多:提供的示例代码仅反射(reflect)了我的大
我是一名优秀的程序员,十分优秀!