我的View Model上的Context Menu命令有些困难.
我正在为View Model中的每个命令实现ICommand接口,然后在View(MainWindow)的资源中创建ContextMenu,并使用MVVMToolkit中的CommandReference访问当前的DataContext(ViewModel)命令.
当我调试应用程序时,似乎除了创建窗口之外,没有调用命令上的CanExecute方法,因此我的Context MenuItems没有像我期望的那样启用或禁用.
我已经制作了一个简单的样本(附在这里),它表明了我的实际应用并总结如下.任何帮助将不胜感激!
这是ViewModel
namespace WpfCommandTest
{
public class MainWindowViewModel
{
private List<string> data = new List<string>{ "One", "Two", "Three" };
// This is to simplify this example - normally we would link to
// Domain Model properties
public List<string> TestData
{
get { return data; }
set { data = value; }
}
// Bound Property for listview
public string SelectedItem { get; set; }
// Command to execute
public ICommand DisplayValue …Run Code Online (Sandbox Code Playgroud) 我面临的情况是我有依赖对象,我希望能够删除一个对象及其所有引用.
假设我有一个类似下面代码的对象结构,其中Branch类型引用了两个节点.
public class Node
{
// Has Some Data!
}
public class Branch
{
// Contains references to Nodes
public Node NodeA
public Node NodeB
}
public class Graph
{
public List<Node> Nodes;
public List<Branch> Branches;
}
Run Code Online (Sandbox Code Playgroud)
如果我从Graph类的Nodes列表中删除一个Node,那么一个或多个Branch对象仍然可能包含对已删除节点的引用,从而将其保留在内存中,而我真的很想设置它对已删除的节点的任何引用都为null并让垃圾收集启动.
除了枚举每个分支并按顺序检查每个节点引用之外,是否有关于如何删除每个分支实例中节点的引用以及引用已删除节点的任何其他类的任何明智想法?
我有一个自定义的WPF Canvas,我想在其上显示一个网格.我这样做是通过覆盖Canvas上的OnRender方法,并使用DrawingContext绘图函数.IsGridVisible,GridWidth,GridHeight分别是水平和垂直每个网格线之间的像素数.
我还使用Canvas.LayoutTransform属性上的ScaleTransform来缩放Canvas项目,并且正如人们所期望的那样,网格线厚度乘以ScaleTransform缩放因子,如下图所示.有没有办法绘制单个像素线,无论当前的Canvas RenderTransform如何?
protected override void OnRender(System.Windows.Media.DrawingContext dc)
{
base.OnRender(dc);
if (IsGridVisible)
{
// Draw GridLines
Pen pen = new Pen(new SolidColorBrush(GridColour), 1);
pen.DashStyle = DashStyles.Dash;
for (double x = 0; x < this.ActualWidth; x += this.GridWidth)
{
dc.DrawLine(pen, new Point(x, 0), new Point(x, this.ActualHeight));
}
for (double y = 0; y < this.ActualHeight; y += this.GridHeight)
{
dc.DrawLine(pen, new Point(0, y), new Point(this.ActualWidth, y));
}
}
}
Run Code Online (Sandbox Code Playgroud)
alt text http://www.freeimagehosting.net/uploads/f05ad1f602.png
在我的WPF应用程序中,我有一个CollectionViewSource,它提供了一个私有ObservableCollection的视图.CollectionViewSource有一个PropertyGroupDescription,它在ListBox中用于向用户的首选项显示数据.
在ListBox GroupStyle中使用包含Expander Control的ControlTemplate,结果非常好.但是,除了Group Name之外,我想在Expander Header中显示每个组中的项目数.关于绑定路径的任何想法?
此致,利亚姆
<Style x:Key="basicGroupStyle" TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander Header="{Binding Name}" IsExpanded="True">
<ItemsPresenter/>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<ListBox ItemsSource="{Binding Source={StaticResource myViewSource}}">
<ListBox.GroupStyle>
<GroupStyle ContainerStyle="{StaticResource basicGroupStyle}"/>
</ListBox.GroupStyle>
</ListBox>
Run Code Online (Sandbox Code Playgroud) 我有一个WPF应用程序,它使用(当前)本地数据库作为绑定源.使用Visual Studio 2010工具,我有一个LINQ-SQL模型,它充当大多数表单的Datacontext.
我有一个带有TextBox和Datagrid的UserControl.使用表在UserControl.Loaded事件上设置datagrid ItemSource.TextBox分配了一个事件,以便在文本更改并在datagrid上更新ItemSource时对数据库执行查询.
这个问题是查询数据库所需的时间.因为我正在为每次搜索重新分配DataGrid项目源.
我应该在UserControl加载时加载所有记录 - 有没有办法在BackgroundWorker或类似工具中异步加载记录?
我需要在每次搜索后重新分配DataGrid ItemsSource,还是更有效的过滤数据的方法?
谢谢.利亚姆
<UserControl x:Class="Tracker.DocumentsView"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<DataGrid AutoGenerateColumns="False" Margin="12,34,12,50" Name="dataGrid1">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=ID}" Header="ID" />
<DataGridTextColumn Binding="{Binding Path=Reference}" Header="Reference" />
<DataGridTextColumn Binding="{Binding Path=Subject}" Header="Subject" />
</DataGrid.Columns>
</DataGrid>
<TextBox HorizontalAlignment="Left" Margin="64,8,0,0" Name="txtSearchBox" VerticalAlignment="Top" Width="224" TextChanged="txtSearchBox_TextChanged" />
<TextBlock Text="Search" HorizontalAlignment="Left" Margin="11,12,0,0" Name="label1" VerticalAlignment="Top" Height="23" />
</Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
码:
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Tracker.Model;
namespace …Run Code Online (Sandbox Code Playgroud)