1
全部 - 嘗試做2件事:我有一個簡單的應用程序,並試圖將我的Viewmodel集合綁定到我的視圖。記住有兩種方法,但它們都卡在它們兩個上:無法綁定到我的ViewModel集合
方法1:使用網格作爲容器來綁定textblock屬性。
方法2:綁定對象的屬性直接在我的視圖中的正文塊(未使用格作爲容器及其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> {"[email protected]", "[email protected]"},
bottomMessage = "Lorem ipsum dolor sit amet",
hypLink = "http://www.abc.com",
blockType = Rule_Model_1.OCCBlock.HardBlock
});
#endregion
}
#endregion
}
Rule_U I.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>
的觀點仍顯示什麼。同樣如上所述 - 我如何使這兩種方法奏效?任何代碼樣品是極大的讚賞
圖像附帶
權 - 我曾經有過使用列表框,但在這種情況下 - 真的不需要列表框 - 其屏幕上的3個文本框(可以包含在網格中)。很快附上截圖 – Patrick
@Patrick gotcha,請看我的編輯。你只需要更新網格中的'DataContext',它應該按原樣工作。 – McGarnagle
只是做了那個..doesnt加載數據:( – Patrick