绑定目标未更新
我已将两个列表框控件绑定到 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>
我的问题是在这种情况下如何更新目标或刷新/重新加载窗口。
感谢您的意见。
I have bound two listbox controls to an xml file (i.e one list box bound to the xml and the other bound to the first listbox). The listboxes display node values within the xml file as a result of the binding. Add, Edit and Delete operations can be made and do persist to the source file, however the target doesn't get updated unless the window is closed and then reopened. The code for the first list box is as follows:
<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>
and for the second 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>
My question is how to update the target or refresh/reload the window in this case.
Thanks for your input.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 ListBox2 中,您要绑定到特定项目 (listBox1.SelectedItem),因此 IsSynchronizedWithCurrentItem 应该为 false。 IsSynchronizedWithCurrentItem 适用于绑定到集合但只需要所选项目的情况。
In ListBox2, you are binding to a specific item (listBox1.SelectedItem) so IsSynchronizedWithCurrentItem should be false. IsSynchronizedWithCurrentItem is for when you are binding to a collection but want only the selected item.