xaml - 绑定(bind)到 Xaml

标签 xaml mvvm binding

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

假设我的 View 模型看起来像

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 中,如何绑定(bind)到 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;
    }

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

所以,我的两个问题是:
  • 单独使用我的 ViewModel 类就足够了吗,还是我还需要告诉 Xaml ViewModel 的每个内部类(在本例中为 Log 类)?例如

    xmlns:myData ="clr-命名空间:BackUps.Logging.ViewModel"
    xmlns:moreData = "clr-命名空间:BackUps.Logging.Logs"
  • 我需要做什么来绑定(bind)标题?
  • 最佳答案

    Binding并不像你想象的那么复杂,你只是没有掌握TreeviewHierarchicalDataTemplate将属性暴露给 XAML ,
    将您的所有域类设置为公共(public),因为它们在公共(public)属性中使用。
    myData 应该引用域类命名空间。例如:在我的例子中是 xmlns:myData="clr-namespace:WpfApplication3" MoreDetails必须是 Log 中的公共(public)属性(property)类(class)。

       <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; }
    }
    

    关于xaml - 绑定(bind)到 Xaml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14561662/

    相关文章:

    c# - MVVM WPF : Get usercontrol name from ViewModel

    c# - DataTemplateSelector CreateContent 抛出 InvalidOperationException

    安卓服务交互

    wpf - Windows Presentation Foundation (WPF) 项目不支持应用程序

    wpf - 使用字符串文字在标签上绑定(bind)字符串格式

    c# - 枢轴选择更改时如何在枢轴项目中加载图像后如何加载音频文件

    WPF MVVM : View information in the model

    swift - 美元符号在 Swift/SwiftUI 中起什么作用?

    c# - ListView SelectedItems 绑定(bind) : why the list is always null

    wpf - 调整边框控制大小