我丢失了绑定...-帮助将其绑定回 [英] I lost the Binding... - help to bind it back

查看:70
本文介绍了我丢失了绑定...-帮助将其绑定回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试构建文件系统查看器,
到现在为止,我都有以下代码:(大部分代码来自 Karl Shifflett 的博客)

xaml:

Hi
I'm trying to build File System Viewer,
and till now I have this code: (Most of it I took from Karl Shifflett's blog)

xaml:

<Window x:Class="MVVM_FileSystemManager_2.Views.MainView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:c="clr-namespace:MVVM_FileSystemManager_2.Commands"
    Height="400" Width="800"
    Title="TreeViewAlaHierarchicalBinding" 
    xmlns:sysIO="clr-namespace:System.IO;assembly=mscorlib"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns:local="clr-namespace:MVVM_FileSystemManager_2.Models">
    
    <Window.Resources>
        <!-- Allows a KeyBinding to be associated with a command defined in the View Model  -->
        <c:CommandReference x:Key="ExitCommandReference" Command="{Binding ExitCommand}" />
        <ObjectDataProvider x:Key="getDrives" MethodName="GetDrives" ObjectType="{x:Type sysIO:DriveInfo}" />
        <local:GetFileSysemInformationConverter x:Key="getFileSysemInformationConverter"/>

        <HierarchicalDataTemplate DataType="{x:Type sysIO:DriveInfo}" ItemsSource="{Binding Converter={StaticResource getFileSysemInformationConverter}}">
            <StackPanel Orientation="Horizontal">
                <Image Width="20" Height="20" Stretch="Fill" Source="/MVVM_FileSystemManager_2;component/Images/diskdrive.png" />
                <TextBlock Text="{Binding Name}" Margin="5,0,0,0"></TextBlock>
            </StackPanel>
        </HierarchicalDataTemplate>

        <HierarchicalDataTemplate DataType="{x:Type sysIO:DirectoryInfo}" ItemsSource="{Binding Converter={StaticResource getFileSysemInformationConverter}}">
            <StackPanel Orientation="Horizontal">
                <Image Width="20" Height="20" Stretch="Fill" Source="/MVVM_FileSystemManager_2;component/Images/folder.png" />
                <TextBlock Text="{Binding Name}" Margin="5,0,0,0" ></TextBlock>   
            </StackPanel>
        </HierarchicalDataTemplate>

    </Window.Resources>
   
    <Window.InputBindings>
        <KeyBinding Key="X" Modifiers="Control" Command="{StaticResource ExitCommandReference}" /> 
    </Window.InputBindings>
    
    <Canvas>
        <Menu DockPanel.Dock="Top">
            <MenuItem Header="_File">
                <MenuItem Command="{Binding ExitCommand}" Header="E_xit" InputGestureText="Ctrl-X" />
            </MenuItem>
        </Menu>
        <ListView Margin="0,0,6,35" HorizontalAlignment="Right" Width="355" ItemsSource="{Binding FileList}"  Height="298" Canvas.Left="423" Canvas.Top="25"></ListView>        
        <Grid DataContext="{StaticResource getDrives}"  Height="298" Width="417" Canvas.Left="0" Canvas.Top="25">
            <TreeView ItemsSource="{Binding}"></TreeView>
        </Grid>
        <StackPanel Orientation="Horizontal" Height="25" Canvas.Left="12" Canvas.Top="329" Width="766">
            <Label Content="You are in: " HorizontalAlignment="Left" Width="75"></Label>
            <Label Content="{Binding Path=DirectoryInfoTB}" Width="692"></Label>
        </StackPanel>
    </Canvas>
</Window>

viewModel:

viewModel:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;

using MVVM_FileSystemManager_2.Commands;

namespace MVVM_FileSystemManager_2.ViewModels
{
    public class MainViewModel : ViewModelBase
    {
        private DelegateCommand exitCommand;
        private bool isSelected;
        private string directoryInfoTB;
        private string fileList;


        public MainViewModel()
        {
        }




        public ICommand ExitCommand
        {
            get
            {
                if (exitCommand == null)
                {
                    exitCommand = new DelegateCommand(Exit);
                }
                return exitCommand;
            }
        }

      

        private void Exit()
        {
            Application.Current.Shutdown();
        }

        public string DirectoryInfoTB 
        {
            get { return this.directoryInfoTB; } 
            set     
            { 
                this.directoryInfoTB = value;
           
                OnPropertyChanged("DirectoryInfoTB");
            }
        }

       

        public string FileList
        {
            get { return this.fileList; }
            set 
            {
                this.fileList = value; 
                OnPropertyChanged("FileList");
            }
        }
    
    }
}


GetFileSystemInformationConverter:


GetFileSystemInformationConverter:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Data;
using MVVM_FileSystemManager_2.ViewModels;

namespace MVVM_FileSystemManager_2.Models
{
    public class GetFileSysemInformationConverter : IValueConverter
    {
        DirectoryInfo di;
  

        MainViewModel _vm = new MainViewModel(); 
        public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            //string str = (((System.IO.DirectoryInfo)(System.IO.DirectoryInfo)(value)).Parent).FullName;
            try
            {
                if (value is DriveInfo)
                {
                    di = new DirectoryInfo(((DriveInfo)value).RootDirectory.Name);
                    return di.GetDirectories();
                }
                else
                    if (value is DirectoryInfo)
                    {
                        string str = (string)((DirectoryInfo)value).FullName;
                        _vm.FileList = str;
                        return ((DirectoryInfo)value).GetDirectories();
                    }
                    else
                    {
                        return null;
                    }
            }
            catch (Exception ex)
            {

                return null;
            }
        }

      

        public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
}

我的问题是我丢失"了View的datacontext到viewModel上,我无法在其上绑定属性-不是StackPanel中的标签,
也不是ListView
(我注意到当我设置属性时调用OnPropertyChange时,例如从GetFileSystemInformationConverter类中获取,而不是从ViewModelBase类中获取-OnPropertyChanged中的句柄为空-
我该如何解决?如何将视图的数据上下文再次设置为viewModel?

my problem is that I "lost" the View's datacontext to the viewModel and I cant bind the properties on it - not the label in the StackPanel,
nor the ListView
(I notice that when I call to OnPropertyChange when I set the the property for example from GetFileSystemInformationConverter class than in the ViewModelBase class - the handle in OnPropertyChanged is null -
How can I fix it? How to set again the view's datacontext as the viewModel?

推荐答案

您好,

您是否在后面的代码中将窗口的DataContext设置为MainViewModel呢?

此外,我注意到您将Canvas用作所有容器,但使用菜单上的DockPanel.Top.您可能希望将Canvas更改为DockPanel.
Hi,

do you set the window's DataContext to the MainViewModel in code behind? This is not clear from the XAML. In the XAML, you don't set any datacontext except for the Grid.

Also, I noticed that you are using a Canvas as container for everything, but use DockPanel.Top on the menu. You might want to change the Canvas to a DockPanel.


这篇关于我丢失了绑定...-帮助将其绑定回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆