【问题标题】:ListView two way compiled binding (x:Bind)ListView 两种方式编译绑定(x:Bind)
【发布时间】:2015-08-19 07:03:20
【问题描述】:

我想显示几个名称,并且我希望它们是可编辑的。 所以我使用了 ObservableColection,并使用新的 x:Bind 功能将其绑定到 ListView。

这是我的 XAML:

    <ListView>
        <ListView ItemsSource="{x:Bind ViewModel.Players}">
                <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            </Style>
        </ListView.ItemContainerStyle>

        <ListView.ItemTemplate>
            <DataTemplate xmlns:model="using:Flechette.Model"  x:DataType="model:Player">
                <TextBox Text="{x:Bind Name, Mode=TwoWay}" />
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

还有我的代码:

public sealed partial class GameSettingsPage : Page
{
    ViewModel.GameSettingsViewModel ViewModel { get; set; }

    public GameSettingsPage()
    {
        InitializeComponent();
        DataContextChanged += (s, e) => ViewModel = DataContext as ViewModel.GameSettingsViewModel;
    }
}

问题是 TwoWay 绑定无法编译,我收到错误 CS1061 'WeakReference' 不包含 'LostFocus' 的定义,并且没有扩展方法 'LostFocus' 接受类型为 'WeakReference' 的第一个参数找到(您是否缺少 using 指令或程序集引用?)

我该如何解决?

【问题讨论】:

  • 你能显示完整的 xaml。你在对 LostFocus 事件进行 x:Bind 吗?
  • The full XAML code 如您所见,我想编辑ListView中的元素。它适用于经典绑定,但不适用于 x:Bind
  • 如果您将 TwoWay 绑定更改为 OneWay,您会遇到问题吗?
  • 不,它适用于 OneWay

标签: c# xaml listview binding windows-10


【解决方案1】:

这似乎是 Windows 10 SDK 预览版中的一个问题。给定以下代码:

MainPage.xaml:

     <ListView x:Name="Players">
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            </Style>
        </ListView.ItemContainerStyle>

        <ListView.ItemTemplate>
            <DataTemplate x:DataType="local:Player">
                <TextBox Text="{x:Bind Name, Mode=TwoWay}" />
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

MainPage.xaml.cs:

    private ObservableCollection<Player> players = new ObservableCollection<Player>();
    public MainPage()
    {
        this.InitializeComponent();
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        this.players.Add(new Player());
        this.players.Add(new Player());
        this.players.Add(new Player());
        this.players.Add(new Player());
        this.players.Add(new Player());

        this.Players.ItemsSource = players;
    }

Player.cs:

public class Player : INotifyPropertyChanged
{
    private string name;

    public string Name
    {
        get { return name; }
        set
        {
            if (value == name) return;
            name = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

编译和应用程序工作并提供预期的行为:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-24
    • 2016-02-27
    • 2015-12-11
    • 1970-01-01
    • 2017-09-05
    • 2014-02-13
    • 1970-01-01
    相关资源
    最近更新 更多