绑定目标未更新

发布于 2024-10-27 16:41:03 字数 3020 浏览 2 评论 0原文

我已将两个列表框控件绑定到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情泪▽动烟 2024-11-03 16:41:03

在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文