SelectedItem更改文本块变量

时间:2016-06-12 14:23:41

标签: c# wpf xaml listview mvvm

我希望将ObservableCollection中用作ListView的对象的所有数据都收集起来。 ListView工作正常我希望在选择项目TextBlockListView之外)中的信息时,使用该对象的信息进行更新。

我该怎么办?我做错了什么?

所以代码:

<ListView 
   x:Name="dataGrid" 
   ItemsSource="{Binding Friends}" 
   Height="586" 
   BorderThickness="0" 
   SelectedItem="{Binding SelectedItemFriends,Mode=TwoWay}"
   HorizontalAlignment="Left" Width="460">
   <ListView.ItemTemplate>
      <DataTemplate>
         <StackPanel Orientation="Horizontal">
            <Image Source="Resources\Images\ic_status.png" Height="24" Width="18"/>
            <StackPanel Margin="5" Orientation="Vertical">
               <TextBlock FontWeight="Bold" Text="{Binding name}"/>
               <StackPanel x:Name="RemoveItems" Margin="5" Orientation="Vertical">
                  <TextBlock Text="{Binding lastLocation, StringFormat='Location: {0}'}"/>
                  <TextBlock Text="{Binding timestamp}"/>
               </StackPanel>
            </StackPanel>
         </StackPanel>
      </DataTemplate>
   </ListView.ItemTemplate>
</ListView>
<StackPanel x:Name="AdditionItems" Margin="5" Orientation="Vertical" Visibility="Visible">
   <TextBlock>
      <TextBlock.Text>
         <MultiBinding Converter="{StaticResource myConverter}">
            <Binding ElementName="dataGrid" Path="loc.country"/>
            <Binding ElementName="dataGrid" Path="loc.area"/>
         </MultiBinding>
      </TextBlock.Text>
   </TextBlock>
</StackPanel>

1 个答案:

答案 0 :(得分:2)

您应该在绑定中指定SelectedItem属性:

<TextBlock>
      <TextBlock.Text>
         <MultiBinding Converter="{StaticResource myConverter}">
            <Binding ElementName="dataGrid" Path="SelectedItem.loc.country"/>
            <Binding ElementName="dataGrid" Path="SelectedItem.loc.area"/>
         </MultiBinding>
      </TextBlock.Text>
   </TextBlock>