数据绑定在 silverlight 中不起作用
Data Binding not working in silverlight
我正在尝试进行一些简单的数据绑定。我有一个集合,其中 returns 一个带有 属性 的项目称为 MenuName。我已经检查过它是否正确返回。这就是我尝试进行绑定的方式。 (通过 Menu 继承自 INotifyPropertyChanged。)
XAML
<Grid x:Name="LayoutRoot" DataContext="MenuItems">
<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Grid.Row="0" Margin="24,17,0,28">
<TextBlock Text="Test" Typography.Capitals="SmallCaps"/>
<TextBlock Text="{Binding MenuName }" Margin="0,12,0,0" FontSize="52"/>
<CheckBox>Cache</CheckBox>
</StackPanel>
</Grid>
隐藏代码:
#region Members
MyAppWinConnectionClient MyAppWinService;
#endregion Members
#region Properties
public ObservableCollection<Menu> MenuItems { get; set; }
#endregion Properties
public StandardUI()
{
MyAppWinService = new MyAppWinConnectionClient();
this.InitializeComponent();
LoadTest();
}
private async void LoadTest()
{
try
{
MenuItems = await MyAppWinService.GetMenuEntriesAsync();
}
catch (FileNotFoundException ex)
{
}
}
我想我遗漏了一些明显的东西。你怎么看?
我建议您使用 "StandardUI" 作为视图(或 LayoutRoot)的 DataContext,然后使用 "MenuItems" 作为 StackPanel 的 ItemsSource。然后,您可以根据需要向 StandardUI 添加许多属性,并在其他控件上使用它们。像 mvvm 模式。 ;)
使用页面的 DataContext
,如果未设置其数据上下文,每个控件将使用该页面。将页面的数据上下文设置为:
public StandardUI()
{
DataContext = this;
MyAppWinService = new MyAppWinConnectionClient();
this.InitializeComponent();
LoadTest();
}
然后在绑定上,提取第一个菜单项:
<Grid x:Name="LayoutRoot">
<StackPanel>
<TextBlock Text="Test" Typography.Capitals="SmallCaps"/>
<TextBlock Text="{Binding MenuItems[0].MenuName }" />
<CheckBox>Cache</CheckBox>
</StackPanel>
</Grid>
但是应该研究一下 MVVM。我在标题为 Xaml: ViewModel Main Page Instantiation and Loading Strategy for Easier Binding.
的博客文章中给出了绑定、数据上下文和 MVVM 的简短示例
我正在尝试进行一些简单的数据绑定。我有一个集合,其中 returns 一个带有 属性 的项目称为 MenuName。我已经检查过它是否正确返回。这就是我尝试进行绑定的方式。 (通过 Menu 继承自 INotifyPropertyChanged。)
XAML
<Grid x:Name="LayoutRoot" DataContext="MenuItems">
<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Grid.Row="0" Margin="24,17,0,28">
<TextBlock Text="Test" Typography.Capitals="SmallCaps"/>
<TextBlock Text="{Binding MenuName }" Margin="0,12,0,0" FontSize="52"/>
<CheckBox>Cache</CheckBox>
</StackPanel>
</Grid>
隐藏代码:
#region Members
MyAppWinConnectionClient MyAppWinService;
#endregion Members
#region Properties
public ObservableCollection<Menu> MenuItems { get; set; }
#endregion Properties
public StandardUI()
{
MyAppWinService = new MyAppWinConnectionClient();
this.InitializeComponent();
LoadTest();
}
private async void LoadTest()
{
try
{
MenuItems = await MyAppWinService.GetMenuEntriesAsync();
}
catch (FileNotFoundException ex)
{
}
}
我想我遗漏了一些明显的东西。你怎么看?
我建议您使用 "StandardUI" 作为视图(或 LayoutRoot)的 DataContext,然后使用 "MenuItems" 作为 StackPanel 的 ItemsSource。然后,您可以根据需要向 StandardUI 添加许多属性,并在其他控件上使用它们。像 mvvm 模式。 ;)
使用页面的 DataContext
,如果未设置其数据上下文,每个控件将使用该页面。将页面的数据上下文设置为:
public StandardUI()
{
DataContext = this;
MyAppWinService = new MyAppWinConnectionClient();
this.InitializeComponent();
LoadTest();
}
然后在绑定上,提取第一个菜单项:
<Grid x:Name="LayoutRoot">
<StackPanel>
<TextBlock Text="Test" Typography.Capitals="SmallCaps"/>
<TextBlock Text="{Binding MenuItems[0].MenuName }" />
<CheckBox>Cache</CheckBox>
</StackPanel>
</Grid>
但是应该研究一下 MVVM。我在标题为 Xaml: ViewModel Main Page Instantiation and Loading Strategy for Easier Binding.
的博客文章中给出了绑定、数据上下文和 MVVM 的简短示例