我有以下Windows RT应用程序.我将一个字符串列表绑定到TextBlocks的ItemsControl.这将显示空字符串"System.Collections.Generic.List'1 [System.String]"而不是空字符串.我希望它显示一个空字符串而不是DataContext的类型.
代码背后:
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
DataContext = new List<string>() { "", "not empty string" };
}
}
Run Code Online (Sandbox Code Playgroud)
XAML:
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<ItemsControl ItemsSource="{Binding}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" FontSize="25"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
Run Code Online (Sandbox Code Playgroud)
输出:
System.Collections.Generic.List'1[System.String]
non empty string
Run Code Online (Sandbox Code Playgroud)
我用传统的wpf做了同样的例子,它正确地显示了空字符串.
编辑 这输出同样的事情.
代码背后:
public class Model
{
private readonly List<string> items = new List<string>() { "", "non empty string" };
public List<string> Items
{
get …Run Code Online (Sandbox Code Playgroud) 我正在尝试将ListViewItem的IsSelected属性绑定到ViewModel中的属性.它在WPF中工作正常,但在Windows RT中,IsSelected属性永远不会被设置.
public class Item : INotifyPropertyChanged
{
private readonly string name;
private bool isSelected;
public event PropertyChangedEventHandler PropertyChanged;
public bool IsSelected
{
get { return isSelected; }
set { isSelected = value; RaisePropertyChanged("IsSelected"); }
}
public string Name { get { return name; } }
public Item(string name)
{
this.name = name;
}
protected void RaisePropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
handler.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class ViewModel
{
private readonly ObservableCollection<Item> items …Run Code Online (Sandbox Code Playgroud)