小编gen*_*nus的帖子

使用HierarchicalDataTemplate的WPF菜单绑定不能正确呈现菜单项

我正在构建一个WPF应用程序,试图密切关注MVVM原则.我在使菜单正确渲染时遇到问题.我已经尝试了一些方法而且卡住了.看起来我的绑定是正确的,但我不确定我的样式操作.

这是我遇到问题的代码.就像我说的,似乎绑定是好的,我甚至可以使用Snoop看到Header菜单项的正确值,但我看到的只是菜单项的空容器.

 <DockPanel>
        <DockPanel.Resources>
            <HierarchicalDataTemplate x:Key="TopMenuHDT" ItemsSource="{Binding Children}">
                <HierarchicalDataTemplate.ItemContainerStyle>
                    <Style TargetType="{x:Type MenuItem}">
                        <Setter Property="Command" Value="{Binding Command}" />
                        <Setter Property="Header" Value="{Binding MenuText}" />
                        <Setter Property="Icon">
                            <Setter.Value>
                                <Image Source="{Binding MenuIcon}" Height="16px" Width="16px" />
                            </Setter.Value>
                        </Setter>
                    </Style>
                </HierarchicalDataTemplate.ItemContainerStyle>
            </HierarchicalDataTemplate>

        </DockPanel.Resources>
        <Menu DockPanel.Dock="Top" Height="auto"
              ItemsSource="{Binding TopMenuItems}" 
              ItemTemplate="{StaticResource TopMenuHDT}"/>
Run Code Online (Sandbox Code Playgroud)

在我的主ViewModel中:

    private ObservableCollection<MenuViewModel> _topMenuItems;
    public ObservableCollection<MenuViewModel> TopMenuItems
    {
        get { return _topMenuItems; }
        set
        {
            if (_topMenuItems == value)
                return;

            _topMenuItems = value; base.RaisePropertyChanged("TopMenuItems");
        }
    }
...
    public void LoadMainMenu()
    {
        IList<ViewModels.MenuViewModel> fileMenuItems = PopulateFileMenuEntries();
        IList<ViewModels.MenuViewModel> …
Run Code Online (Sandbox Code Playgroud)

wpf binding menu

7
推荐指数
1
解决办法
5527
查看次数

DependencyProperties未在UserControl中按预期触发回调

我有一个非常简单的例子,DependencyProperty我已经在UserControl上注册了,它没有按预期工作.

我将此属性绑定到MainWindow中的DP(称为Test),它似乎OnPropertyChanged每次更改时触发事件,但是DependencyProperty我的UserControl中作为此绑定的目标,似乎只在第一次通知此属性时才会收到通知改变.

这是我在代码中尝试做的事情:

我的UserControl:

public partial class UserControl1 : UserControl
{
    public static readonly DependencyProperty ShowCategoriesProperty = 
        DependencyProperty.Register("ShowCategories", typeof(bool), typeof(UserControl1), new FrameworkPropertyMetadata(false, OnShowsCategoriesChanged));

    public UserControl1()
    {
        InitializeComponent();
    }

    public bool ShowCategories
    {
        get
        {
            return (bool)GetValue(ShowCategoriesProperty);
        }
        set
        {
            SetValue(ShowCategoriesProperty, value);
        }
    }

    private static void OnShowsCategoriesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        //this callback is only ever hit once when I expect 3 hits...

        ((UserControl1)d).ShowCategories = (bool)e.NewValue;
    }
}
Run Code Online (Sandbox Code Playgroud)

MainWindow.xaml.cs

public partial class MainWindow : …
Run Code Online (Sandbox Code Playgroud)

c# wpf binding user-controls dependency-properties

5
推荐指数
1
解决办法
2933
查看次数

WPF MVVM 绑定到 UserControl 的 DependencyProperty 不起作用

我试图坚持使用 MVVM 方法来构建我的 WPF 应用程序,但遇到了一个奇怪的绑定问题,感觉我错过了一些东西。

我有一个用户控件 (PluginsTreeView),它有一个驱动它的 ViewModel (PluginsViewModel)。PluginsTreeView 公开了一个字符串类型 (DocumentPath) 的公共 DependencyProperty。我的 MainWindow 在 XAML 中设置了这个属性,但它似乎没有将它添加到我的 UserControl 中。我正在寻找一些关于为什么这不起作用的迹象。

插件TreeView.xaml.cs

public partial class PluginsTreeView: UserControl
{
    public PluginsTreeView()
    {
        InitializeComponent();
        ViewModel = new ViewModels.PluginsViewModel();
        this.DataContext = ViewModel;
    }

    public static readonly DependencyProperty DocumentPathProperty =
        DependencyProperty.Register("DocumentPath", typeof(string), typeof(PluginsTreeView), new FrameworkPropertyMetadata(""));


    public string DocumentPath
    {
        get { return (string)GetValue(DocumentPathProperty); }
        set 
        {
            //*** This doesn't hit when value set from xaml, works fine when set from code behind
            MessageBox.Show("DocumentPath"); 
            SetValue(DocumentPathProperty, value); 
            ViewModel.SetDocumentPath(value); …
Run Code Online (Sandbox Code Playgroud)

wpf xaml dependency-properties mvvm

5
推荐指数
1
解决办法
3531
查看次数

ASP.NET MVC3 View模型不是null,但在发回Controller时缺少数据

这是我的情况:

我有一个名为OrderFromCategory的控制器操作,它使用数据填充视图模型(在这种情况下,视图模型只是另一个视图模型类型的项列表).这将传递给强类型视图,该视图显示项目列表并允许用户更改每个项目的数量.在Post上,我的post控制器操作会返回模型对象,但列表中没有对象.我错过了接线的东西吗?不知所措,任何帮助将不胜感激!非常感谢!

另外,我的验证是否正确连线?在我得到模型数据持久之前无法测试!

这是代码:

视图模型:

public class MenuItemsModel
{
    public MenuItemsOrderModel()
    {
        items = new List<MenuItemOrderModel>();
    }

    public List<MenuItemOrderModel> items { get; set; }
}

public class MenuItemOrderModel
{
    public int ItemID { get; set; }

    [Display(Name = "Name")]
    public string ItemName { get; set; }

    [Display(Name = "Description")]
    public string Description { get; set; }

    [Display(Name = "Price")]
    [DataType(DataType.Currency)]
    public decimal Price { get; set; }

    [Display(Name = "Quantity")]
    public int Quantity { get; set; }

    public string Qualifier …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc asp.net-mvc-3

4
推荐指数
1
解决办法
3593
查看次数