【问题标题】:Bound target not updating绑定目标未更新
【发布时间】:2011-03-26 01:30:20
【问题描述】:

我已将两个列表框控件绑定到一个 xml 文件(即一个列表框绑定到 xml,另一个绑定到第一个列表框)。作为绑定的结果,列表框在 xml 文件中显示节点值。可以进行添加、编辑和删除操作并将其保留到源文件,但是目标不会更新,除非窗口关闭然后重新打开。第一个列表框的代码如下:

<ListBox Name="listBox1" Width="224" Height="115" Margin="0,0,0,5" 
IsSynchronizedWithCurrentItem="False" HorizontalAlignment="Stretch" 
VerticalAlignment="Stretch" Style="{StaticResource ListBoxStyle1}"
ItemsSource="{Binding Source={StaticResource RecipeList}, XPath=recipeType,
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
     <ListBox.ItemTemplate>
           <DataTemplate>
                <Grid Background="Black">
                      <Grid.ColumnDefinitions>
                           <ColumnDefinition Width="40"/>
                           <ColumnDefinition Width="170"/>
                      </Grid.ColumnDefinitions>
                      <StackPanel Grid.Column="1" Height="35" Background="#181818">
                           <TextBlock Background="Black" Margin="1" FontWeight="Bold" 
                           Padding="7" Height="33" Width="184" Foreground="#D0D0D0">
                                 <TextBlock.Text>
                                      <MultiBinding StringFormat="{}{0}">
                                           <Binding XPath="@description" />
                                      </MultiBinding>
                                 </TextBlock.Text>
                            </TextBlock>
                      </StackPanel>
                 </Grid>
           </DataTemplate>
      </ListBox.ItemTemplate>
</ListBox>

对于第二个列表框:

<ListBox Name="listBox2" Width="218" Height="144" Margin="0,0,0,5"
IsSynchronizedWithCurrentItem="True" Style="{StaticResource ListBoxStyle1}"
ItemsSource="{Binding ElementName=listBox1, Path=SelectedItem, Mode=OneWay}">
     <ListBox.ItemTemplate>
         <DataTemplate>
              <StackPanel Height="auto" Background="#181818">
                    <TextBlock Padding="7">
                          <StackPanel Background="Black" Margin="-5.5,-5.5,-6,-6.8">
                                <TextBlock Width="210" FontWeight="Bold" FontSize="12"
                                HorizontalAlignment="Left" Text="{Binding XPath=name}"
                                Background="Black" Foreground="#D0D0D0" Padding="0"                 Margin="5,0,0,3" />
                                <TextBlock Width="187" FontSize="11" HorizontalAlignment="Left"
                                Text="{Binding XPath=summary}" Background="Black" Foreground="Gray" 
                                Margin="5,0,0,5" />
                          </StackPanel>
                    </TextBlock>
              </StackPanel>
         </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

我的问题是在这种情况下如何更新目标或刷新/重新加载窗口。

感谢您的意见。

【问题讨论】:

  • 按目标,你的意思是listBox2

标签: c# wpf xaml binding target


【解决方案1】:

在 ListBox2 中,您将绑定到特定项目 (listBox1.SelectedItem),因此 IsSynchronizedWithCurrentItem 应该为 false。 IsSynchronizedWithCurrentItem 适用于当您绑定到集合但只需要选定项目时。

【讨论】:

  • listbox1绑定到xml文件的数据源,listbox2绑定到listbox1作为itemsource。一旦进行操作,xml文件就会更新,绑定的控件也应该更新。所需的效果是刷新/重新加载绑定控件,因此 listbox2 将像 listbox1 一样更新。 listbox1 的值是静态的,而 listbox2 的值不是静态的,这表明 xml 文件和这些控件显示的 xml 元素。查看示例 xml 文件:
  • Omelet on Toast一顿丰盛的早餐两个鸡蛋,一条面包,..拿鸡蛋和...
猜你喜欢
  • 2022-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-05
  • 2021-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多