Wpf 无法绑定到我的ViewModel集合
全部-尝试做两件事:我有一个简单的应用程序,并试图将我的Viewmodel集合绑定到我的视图。考虑到两种方法,但我对这两种方法都感到困惑: 方法1:使用网格作为容器绑定textblock属性 方法2:将对象的属性直接绑定到我的视图上的textblock(不使用网格作为容器及其Datacontext属性) 规则模型1.csWpf 无法绑定到我的ViewModel集合,wpf,xaml,mvvm,Wpf,Xaml,Mvvm,全部-尝试做两件事:我有一个简单的应用程序,并试图将我的Viewmodel集合绑定到我的视图。考虑到两种方法,但我对这两种方法都感到困惑: 方法1:使用网格作为容器绑定textblock属性 方法2:将对象的属性直接绑定到我的视图上的textblock(不使用网格作为容器及其Datacontext属性) 规则模型1.cs public class Rule_Model_1 { public string topMessage { get; set; } public List&l
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
}
}
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
}
public partial class Rule_UI_1 : UserControl
{
Rule_VM_1 rulevm1;
public Rule_UI_1()
{
InitializeComponent();
rulevm1 = new Rule_VM_1();
DataContext = rulevm1;
}
}
规则_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>
视图仍然没有显示任何内容。同样如上所述-我如何使这两种方法都起作用?非常感谢任何代码示例
图像附加问题在于,您需要一个列表视图来绑定到集合属性(
规则
)。这应该起作用:
<ListBox Grid.Column="1" ItemsSource="{Binding rule}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding topmessage}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
如果没有网格:
<TextBlock Text="{Binding rule[0].topmessage}" />
问题是您需要一个列表视图来绑定到集合属性(
规则
)。这应该起作用:
<ListBox Grid.Column="1" ItemsSource="{Binding rule}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding topmessage}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
如果没有网格:
<TextBlock Text="{Binding rule[0].topmessage}" />
right-我让它使用listbox,但在本例中-真的不需要listbox-屏幕上有3个文本块(可以包含在网格中)。附加屏幕截图shortly@Patrick明白了,请看我的编辑。您只需要更新网格中的DataContext
,它应该按原样工作。只需这样做..不加载数据:(尝试了这两个选项-但是(使用和不使用网格)-似乎没有显示任何数据确定-重试。似乎有效…只是清理解决方案和重建似乎有帮助:(.网格解决方案现在运行得非常好。还尝试了textblock解决方案!!谢谢dbaseman!!!对-我使用了listbox,但在这种情况下-真的不需要listbox-屏幕上的3个文本块(可以包含在网格中).附加屏幕截图shortly@Patrick明白了,请查看我的编辑。你只需要更新网格中的DataContext
,它应该按原样工作。就这样做了..不加载数据:(尝试了两个选项-但是(有网格和没有网格)-似乎没有显示任何数据确定-再试一次。似乎有效…只是清理解决方案和重建似乎有帮助:(.网格解决方案现在工作得非常好。还尝试textblock解决方案!!谢谢dbaseman!!!