wpf - 无法绑定(bind)到我的 ViewModel 的集合

标签 wpf xaml mvvm

全部 - 尝试做两件事:我有一个简单的应用程序,并试图将我的 Viewmodel 集合绑定(bind)到我的 View 。有两种方法,但我都陷入了困境:

方法一:使用 Grid 作为容器来绑定(bind) textblock 属性。

方法 2:将对象的属性直接绑定(bind)到我 View 上的文本 block (不使用网格作为容器及其 ​​Datacontext 属性)

Rule_Model_1.cs

public class Rule_Model_1
{
    public string topMessage { get; set; }
    public List<string> recipients { get; set; }
    public string bottomMessage { get; set; }
    public string hypLink { get; set; }
    public OCCBlock blockType { get; set; }

    public enum OCCBlock
    {
        HardBlock,
        SoftBlock,
        ModifyAndSend
    }
}

Rule_VM_1.cs
class Rule_VM_1
{
    #region Properties
    public List<Rule_Model_1> rule { get; set; }
    #endregion

    #region Constructor
    public Rule_VM_1()
    {
        #region Initializing a Rule
        rule = new List<Rule_Model_1>();

        rule.Add(new Rule_Model_1()
        {
            topMessage = "Lorem ipsum dolor sit amet.",
            recipients = new List<string> {"pr@hotmail.com", "pra@gmail.com"},
            bottomMessage = "Lorem ipsum dolor sit amet",
            hypLink = "http://www.abc.com",
            blockType = Rule_Model_1.OCCBlock.HardBlock
        });
        #endregion
    }
    #endregion
}

Rule_UI.xaml.cs
public partial class Rule_UI_1 : UserControl
{
    Rule_VM_1 rulevm1;
    public Rule_UI_1()
    {
        InitializeComponent();
        rulevm1 = new Rule_VM_1();
        DataContext = rulevm1; 
    }
}

Rule_UI.xaml
  <UserControl x:Class="OCC_WPF_POC.Rule_UI_1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d">
<GroupBox Header="Rule Details">
    <Grid HorizontalAlignment="Center" VerticalAlignment="Center">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>


        <Grid Grid.Column="1" DataContext="{Binding rule}">
            <Grid.RowDefinitions>
                <RowDefinition Height="50"></RowDefinition>
                <RowDefinition Height="200"></RowDefinition>
                <RowDefinition Height="50"></RowDefinition>
            </Grid.RowDefinitions>
            <TextBlock Grid.Row="0" Text="{Binding topmessage}" />
        </Grid>
    </Grid>
</GroupBox>

该 View 仍然没有显示任何内容。同样如上所述 - 我如何让这两种方法都起作用?非常感谢任何代码示例

附图片Window Image

最佳答案

问题是您需要一个 ListView 来绑定(bind)到集合属性( rule )。这应该有效:

<ListBox Grid.Column="1" ItemsSource="{Binding rule}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding topmessage}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

要绑定(bind)到网格,可以设置 DataContext使用索引器 [] :
<Grid Grid.Column="1" DataContext="{Binding rule[0]}">
    <TextBlock Grid.Row="0" Text="{Binding topmessage}" />
</Grid>

没有网格:
<TextBlock Text="{Binding rule[0].topmessage}" />

关于wpf - 无法绑定(bind)到我的 ViewModel 的集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12252166/

相关文章:

c# - 如何从绑定(bind)到 CollectionViewSource 的列表框中找到正确的项目

ios - 在 MVVM + Coordinator 中,如何处理 subview ?

wpf - 从标签中删除 WPF 验证装饰

wpf - GridView 与 DataGrid 性能对比

wpf - 当后面的代码更改绑定(bind)源时更新 UI

wpf - VisualStateManager VisualGroup 优先级

.net - XmlnsDefinition似乎不起作用

c# - 在 C# 的 ListBox 中添加 ListBoxItem?

c# - WPF ListboxItem 和上下文菜单

wpf - MEF WPF - 通过插件取消 Application.Current.Shutdown()