1

我正在将模型绑定到我的 Xaml 代码,并且对如何绑定到属性有疑问。

假设我的视图模型看起来像

internal class LogsVM
{
    private List<Log> logList;
    public List<Log> LogList
    {
        get; set;
    }       

    public LogsVM()
    {

    }

    public LogsVM(List<Logging.Log> logs)
    {
        logList = logs;
    }
}

并假设我的 Log 类看起来像

internal class Log
{
    public string Title { get;set; }
    public List<MoreDetails> moreDetails;

    public Log()
    {
        moreDetails= new List<MoreDetails>();
    }
}

在 Xaml 中,如何绑定到 TreeView 中的标题?

到目前为止,我的 Xaml 看起来像

<Window x:Class="BackUps.Logging.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:myData ="clr-namespace:BackUps.Logging.ViewModel"
        Title="Logging Results" Height="350" Width="525">

    <Grid>
        <Grid.Resources>
            <myData:LogsVM x:Key="Vm" />
        </Grid.Resources>
        <Grid.DataContext>
            <Binding Source="{StaticResource Vm}"></Binding>
        </Grid.DataContext>
        <TreeView>
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate DataType="{x:Type myData:LogsVM}" ItemsSource="{Binding LogList}">
                    <TextBlock Text="{Binding Title}" />
                    <HierarchicalDataTemplate.ItemTemplate>
                        <DataTemplate DataType="{x:Type myData:LogsVM}">
                            <TextBlock Text="{Binding moreDetails.Staus}" />
                        </DataTemplate>
                    </HierarchicalDataTemplate.ItemTemplate>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>
    </Grid>
</Window>

我的 MainWindow 代码在后面

public MainWindow(List<Log> logs)
    {
        InitializeComponent();
        LogsVM logVm = new LogsVM(logs);
        this.DataContext = logVm;
    }

正如您在上面的代码中看到的,我正在尝试绑定 Title 属性,但我的屏幕根本不显示文本。

所以,我的两个问题是:

  1. 单独使用我的 ViewModel 类就足够了吗,还是我还需要告诉 Xaml ViewModel 的每个内部类(在本例中为 Log 类)?例如

    xmlns:myData="clr-命名空间:BackUps.Logging.ViewModel"
    xmlns:moreData = "clr-命名空间:BackUps.Logging.Logs"

  2. 我需要做什么来绑定标题?

4

1 回答 1

2

Binding并不像您想象的那样复杂,您只是没有掌握Treeview's 的HierarchicalDataTemplate内容并将属性公开给XAML,请将您的所有域类设置为 public,因为它们在公共属性中使用。myData 应该引用域类命名空间。例如:在我的情况下xmlns:myData="clr-namespace:WpfApplication3" MoreDetails,它必须是类中的公共属性Log

   <TreeView ItemsSource="{Binding LogList}" >
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate DataType="{x:Type myData:Log}" ItemsSource="{Binding MoreDetails}">
                <TextBlock Text="{Binding Title}" />
                <HierarchicalDataTemplate.ItemTemplate >
                    <DataTemplate DataType="{x:Type myData:MoreDetails}" >
                        <TextBlock Text="{Binding Status}" />
                    </DataTemplate>
                </HierarchicalDataTemplate.ItemTemplate>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>



public class Log
    {
        public string Title { get; set; }
        public List<MoreDetails> MoreDetails { get; set; }

        public Log()
        {
            MoreDetails = new List<MoreDetails>();
        }
    }

    public class MoreDetails
    {
        public string Status { get; set; }
    }

public class YourVM
{
     public YourVM() // in my case i've just run it fast in code behind 
            {
                LogList = new List<Log>
                    {
                        new Log{Title = "Hichem", MoreDetails = new List<MoreDetails>{ new MoreDetails{Status = "OK"}}},
                        new Log{Title = "Hichem"},
                        new Log{Title = "Hichem"},
                        new Log{Title = "Hichem"},
                    };

            }


            public List<Log> LogList { get; set; }
}
于 2013-01-28T12:50:47.283 回答