小编Dav*_*mek的帖子

TextBlock绑定显示类名而不是空字符串

我有以下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)

xaml binding microsoft-metro windows-runtime winrt-xaml

10
推荐指数
1
解决办法
1174
查看次数

ListViewItem IsSelected Binding - 适用于WPF,但不适用于WinRT

我正在尝试将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)

c# xaml windows-runtime winrt-xaml

9
推荐指数
1
解决办法
6377
查看次数