2017-05-11 21 views
0

MVVMでアプリケーションを実装しています。今私は潜在的な結果をデータベースに照会するオートコンプリートを作成しようとしています。私は今が午前問題は、私はリストボックスのためのコレクションのプロパティを設定しようとすると、私はこの例外を取得することである。ここではデータバインディング時の例外ListBox

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#の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 
} 

私は、このようにデータバインドした他のプロパティが適切に機能するので、なぜこれが起こっているのか把握しようとしています。誰もがなぜこの例外がスローされ、それを修正する方法を知っていますか?

+2

エラーメッセージはかなり明確です。 ''を 'で置き換えます。それに加えて、ListBoxのItemsSourceを 'MyCollection'にバインドし、ビューモデルのプロパティーは' MyListItems'のように見えます。また、ItemsSourceバインディングで 'Mode = TwoWay'を設定するのは無意味です。 – Clemens

答えて

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

ありがとう、私はそれを逃したとは思わない。 – Informat

関連する問題