【问题标题】:Binding to Xaml绑定到 Xaml
【发布时间】:2013-01-28 12:05:36
【问题描述】:

我正在将模型绑定到我的 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 类)? EG

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

  2. 绑定Title需要做什么?

【问题讨论】:

    标签: xaml mvvm binding


    【解决方案1】:

    Binding 并不像您想象的那样复杂,您只是没有掌握TreeviewHierarchicalDataTemplate 的东西并将属性暴露给XAML, 将所有域类设置为公共,因为它们在公共属性中使用。 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; }
    }
    

    【讨论】:

    • 除非所有数据都是静态的,否则这还应该包括更改通知,在两个类上都使用 INotifyPropertyChanged,并且使用 ObservableCollection 而不是 List。
    • 没错,我只是不想在当前代码中添加“大写”功能
    • 感谢您的回答。我可以假设您的代码仅使用 1 个命名空间吗?我认为我遇到的问题是我的日志是 BackUps.Logging 命名空间的一部分,而我的 ViewModel 是我的 BackUps.Logging.ViewModels 命名空间的一部分......或者这应该没有区别吗?
    • 是的,您不需要使用 VM 的命名空间,因为您已经在后面的代码中设置了 DataContext,您只需要命名空间包含您的业务类即可检索所需的外汇日志类型输入:DataType="{x:Type myTypes:Log}" 需要设置xmlns:myTypes="clr-namespace:BackUps.Logging"
    • 感谢@HichemC 抽出宝贵时间。我遇到的问题是我通过后面和我的 xaml 中的代码进行绑定,因此得到了意想不到的结果!谢谢
    猜你喜欢
    • 2017-02-28
    • 2018-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多