简体   繁体   English

数据绑定ListBox时发生异常

[英]Exception when Data Binding ListBox

I am implementing an application in MVVM. 我正在MVVM中实现一个应用程序。 Right now I am trying to create an autocomplete that queries a database for potential results. 现在,我正在尝试创建自动完成功能,以查询数据库以获取潜在结果。 The issue I am having right now is that when I try to set the Collection Property for the Listbox, I get this exception: 我现在遇到的问题是,当我尝试为列表框设置Collection属性时,出现此异常:

  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>

Here is my implementation: 这是我的实现:

XAML 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# ViewModel C#ViewModel

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
}

I've been trying to figure out why this is happening since other properties that I've databinded this way work properly. 我一直试图弄清为什么会这样,因为我通过这种方式进行数据绑定的其他属性可以正常工作。 Does anyone know why this exception is thrown and how to fix it? 有谁知道为什么抛出此异常以及如何解决它?

<ListBox.ItemTemplate>
    <DataTemplate>
        <TextBlock>
            <TextBlock.Text>
                <MultiBinding StringFormat="{}{0} {1}">
                    <Binding Path="Symbol"/>
                    <Binding Path="Name"/>
                </MultiBinding>
            </TextBlock.Text>
        </TextBlock>
    </DataTemplate>
</ListBox.ItemTemplate>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM