【问题标题】:Exception when Data Binding ListBox数据绑定 ListBox 时的异常
【发布时间】:2017-05-11 16:16:32
【问题描述】:

我正在 MVVM 中实现一个应用程序。现在我正在尝试创建一个自动完成功能来查询数据库以获取潜在结果。我现在遇到的问题是,当我尝试为列表框设置集合属性时,我得到了这个异常:

  System.Windows.Markup.XamlParseException occurred
  HResult=0x80131501
  Message=A 'Binding' cannot be set on the 'Path' property of type 'Binding'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.
  Source=<Cannot evaluate the exception source>

这是我的实现:

XAML

<Border Margin="5,2,5,5" Grid.Row="1" Height="Auto"
                    BorderBrush="DarkBlue"
                    BorderThickness="1" CornerRadius="4">
    <ScrollViewer VerticalScrollBarVisibility="Auto" MaxHeight="40">
        <DockPanel x:Name="DockPanelA" Margin="1,1,1,1" Background="White" Height="Auto">
            ....
            <TextBox x:Name="TextBoxA" Background="{x:Null}" 
                     BorderThickness="0" Width="Auto"
                     CaretBrush="SteelBlue" FontSize="14" Foreground="SteelBlue" FontFamily="Calibri" TextAlignment="Left"
                     TextChanged="TextBoxA_TextChanged"/>
        </DockPanel>
    </ScrollViewer>
</Border>
<ListBox Grid.Row="1" Margin="5,2,5,5" MaxHeight="200" Width="{Binding ActualWidth, ElementName=DockPanelA}"
             ScrollViewer.VerticalScrollBarVisibility="Auto"  HorizontalAlignment="Stretch"
             Background="AliceBlue" BorderBrush="DarkBlue" Foreground="SteelBlue" VirtualizingStackPanel.IsVirtualizing="False"
             ItemsSource="{Binding Path=MyCollection, Mode=TwoWay}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock>
                <TextBlock.Text>
                    <MultiBinding StringFormat="{}{0} {1}">
                        <Binding Path="{Binding Symbol}"/>
                        <Binding Path="{Binding Name}"/>
                    </MultiBinding>
                </TextBlock.Text>
            </TextBlock>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

C# 视图模型

public class ViewModel : INotifyPropertyChanged
{
    private ObservableCollection<ListData> _myListItems;

    ...
    public ObservableCollection<ListData> MyListItems
    {
        get { return _myListItems; }
        set { _myListItems = value; NotifyPropertyChanged("MyListItems"); }
    }
    ...

    protected virtual void NotifyPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            var e = new PropertyChangedEventArgs(propertyName);
            handler(this, e);
        }
    }
    ...
    public void DataSearch(object param)
    {
        ...
        Task t = Task.Run(async () =>
        {
            await Task.Delay(5000);
            try
            {
                currSrc.Token.ThrowIfCancellationRequested();
                IList<ListData> newList = Helper.QueryData(searchString);
                currSrc.Token.ThrowIfCancellationRequested();

                MyListItems = new ObservableCollection<ListData>(newList);
                //Exception is Thrown from this
            }
            catch (Exception e)
            {
            }
        });
        ...
    }
    ...
}
public class InputHandler : ICommand
{
    //View calls this and this calls DataSearch as a result
}

我一直试图弄清楚为什么会发生这种情况,因为我以这种方式绑定的其他属性可以正常工作。有谁知道为什么会抛出这个异常以及如何解决它?

【问题讨论】:

  • 错误信息很清楚。将&lt;Binding Path="{Binding Symbol}"/&gt; 替换为&lt;Binding Path="Symbol"/&gt;。除此之外,您将 ListBox 的 ItemsSource 绑定到 MyCollection,而视图模型属性似乎是 MyListItems。另请注意,在 ItemsSource 绑定上设置 Mode=TwoWay 是没有意义的。

标签: c# wpf xaml mvvm listbox


【解决方案1】:
<ListBox.ItemTemplate>
    <DataTemplate>
        <TextBlock>
            <TextBlock.Text>
                <MultiBinding StringFormat="{}{0} {1}">
                    <Binding Path="Symbol"/>
                    <Binding Path="Name"/>
                </MultiBinding>
            </TextBlock.Text>
        </TextBlock>
    </DataTemplate>
</ListBox.ItemTemplate>

【讨论】:

  • 非常感谢你们,我不敢相信我错过了。
猜你喜欢
  • 2020-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-11
  • 2012-05-23
相关资源
最近更新 更多