类属性数据绑定 [英] class property databinding

查看:67
本文介绍了类属性数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Task类:

Hi, I have got a Task class:

public class Task : INotifyPropertyChanged
{
    private string taskName;
    private string description;
    public string Description
    {
        get { return description; }
        set
        {
            description = value;
            OnPropertyChanged(new PropertyChangedEventArgs("Description"));
        }
    }

    public string TaskName
    {
        get { return taskName; }
        set
        {
            taskName = value;
            OnPropertyChanged(new PropertyChangedEventArgs("TaskName"));
        }

    }
    private ObservableCollection<Task> subTasks;
    public ObservableCollection<Task> SubTasks
    {
        get { return subTasks; }
        set
        {
            subTasks = value;
            OnPropertyChanged(new PropertyChangedEventArgs("SubTasks"));
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, e);
    }
    //ctors
    public Task(string strTaskName)
    {
        TaskName = strTaskName;
        //SubTasks = new ObservableCollection<Task>();
    }
    public Task(string strTaskName, string strDescription)
    {
        TaskName = strTaskName;
        Description = strDescription;
        //SubTasks = new ObservableCollection<Task>();
    }

    public Task(string strTaskName, ObservableCollection<Task> subs)
    {
        TaskName = strTaskName;
        SubTasks = subs;
    }
}



需要使用此XAML将其Description属性绑定到文本框控件的方法:



that needs its Description property bound to a textbox control with this XAML:

<Window x:Class="Task_it.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <TreeView Name="treeTasks" Grid.Column="1" Margin="5">
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Path=SubTasks}">
                    
                    <TextBlock Text="{Binding Path=TaskName}" />
                    
                </HierarchicalDataTemplate>               
            </TreeView.ItemTemplate>
        </TreeView>

        <TextBox  Name="txtDescription" Grid.Column="0" VerticalAlignment="Top" Margin="5" Text="{Binding Path=Description}">     
        </TextBox>
            
       
    </Grid>
</Window>


我是WPF的新手,我不知道出现问题的地方是文本框未显示说明.这是主窗口:


I am new to WPF and I dont know where the problem occurs that the text box is not showing the description. And this is the main window:

InitializeComponent();
Task job11 = new Task("JOB 1.1","This is job 1.1");
            Task job12 = new Task("JOB 1.2", "This is job 1.2");
            Task job13 = new Task("JOB 1.3", "This is job 1.3");
            Task job14 = new Task("JOB 1.4","This is job 1.4");
            Task MainTask = new Task("Main Job", new ObservableCollection<Task>(new List<Task> { job11, job12, job13, job14 }));
            MainTask.Description = "This is my dear MAINTASK";
txtDescription.DataContext = MainTask;
 treeTasks.Items.Add(MainTask);
            treeTasks.FlowDirection = System.Windows.FlowDirection.RightToLeft;

推荐答案

很遗憾,WPF没有SelectedItem读/写依赖项属性.您可以创建一种行为来解决此问题( http://stackoverflow.com/questions/1000040/selecteditem-in-a-wpf-treeview [ ^ ]),或将后面的代码用于SelectedItemChanged.

理想情况下,您应该具有一个带有ItemsSource和SelectedItem属性的视图模型,并将其绑定到每个模型.快速解决方案是使用SelectedItemChanged事件,并将TextBox的Text属性设置为新选定项目的Description:

Unfortunately there is no SelectedItem read/write dependency property for WPF. You can create a behaviour to get around this (http://stackoverflow.com/questions/1000040/selecteditem-in-a-wpf-treeview[^]), or use code behind for the SelectedItemChanged.

Ideally you should have a view model with an ItemsSource and SelectedItem property, and bind to each. The quick fix is to used the SelectedItemChanged event, and set the Text property of the TextBox to the Description of the new selected item:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        InitializeComponent();
        Task job11 = new Task("JOB 1.1", "This is job 1.1");
        Task job12 = new Task("JOB 1.2", "This is job 1.2");
        Task job13 = new Task("JOB 1.3", "This is job 1.3");
        Task job14 = new Task("JOB 1.4", "This is job 1.4");
        Task MainTask = new Task("Main Job", new ObservableCollection<Task>(new List<Task> { job11, job12, job13, job14 }));
        MainTask.Description = "This is my dear MAINTASK";
        txtDescription.DataContext = MainTask;
        treeTasks.Items.Add(MainTask);
        treeTasks.FlowDirection = System.Windows.FlowDirection.RightToLeft;

        treeTasks.SelectedItemChanged += new RoutedPropertyChangedEventHandler<object>(treeTasks_SelectedItemChanged);
    }

    void treeTasks_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
    {
        var task = (Task) e.NewValue;
        txtDescription.Text = task.Description;
    }


这篇关于类属性数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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