<tfoot id='zal7j'></tfoot>

      <small id='zal7j'></small><noframes id='zal7j'>

        • <bdo id='zal7j'></bdo><ul id='zal7j'></ul>
        <legend id='zal7j'><style id='zal7j'><dir id='zal7j'><q id='zal7j'></q></dir></style></legend>
        <i id='zal7j'><tr id='zal7j'><dt id='zal7j'><q id='zal7j'><span id='zal7j'><b id='zal7j'><form id='zal7j'><ins id='zal7j'></ins><ul id='zal7j'></ul><sub id='zal7j'></sub></form><legend id='zal7j'></legend><bdo id='zal7j'><pre id='zal7j'><center id='zal7j'></center></pre></bdo></b><th id='zal7j'></th></span></q></dt></tr></i><div id='zal7j'><tfoot id='zal7j'></tfoot><dl id='zal7j'><fieldset id='zal7j'></fieldset></dl></div>

        ListBoxItem 产生“System.Windows.Data 错误:4";绑定错误

        时间:2023-10-07

            <tfoot id='dbmIj'></tfoot>
          1. <i id='dbmIj'><tr id='dbmIj'><dt id='dbmIj'><q id='dbmIj'><span id='dbmIj'><b id='dbmIj'><form id='dbmIj'><ins id='dbmIj'></ins><ul id='dbmIj'></ul><sub id='dbmIj'></sub></form><legend id='dbmIj'></legend><bdo id='dbmIj'><pre id='dbmIj'><center id='dbmIj'></center></pre></bdo></b><th id='dbmIj'></th></span></q></dt></tr></i><div id='dbmIj'><tfoot id='dbmIj'></tfoot><dl id='dbmIj'><fieldset id='dbmIj'></fieldset></dl></div>

            <small id='dbmIj'></small><noframes id='dbmIj'>

                  <tbody id='dbmIj'></tbody>
                  <bdo id='dbmIj'></bdo><ul id='dbmIj'></ul>
                  <legend id='dbmIj'><style id='dbmIj'><dir id='dbmIj'><q id='dbmIj'></q></dir></style></legend>
                • 本文介绍了ListBoxItem 产生“System.Windows.Data 错误:4";绑定错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我已经创建了闲置的ListBox:

                  <ListBox x:Name="RecentItemsListBox" Grid.Row="1" BorderThickness="0" Margin="2,0,0,0" SelectionChanged="RecentItemsListBox_SelectionChanged">
                    <ListBox.Resources>
                        <Style TargetType="{x:Type ListBoxItem}"
                               BasedOn="{StaticResource {x:Type ListBoxItem}}">
                            <Style.Triggers>
                                <!--This trigger is needed, because RelativeSource binding can only succeeds if the current ListBoxItem is already connected to its visual parent-->
                                <Trigger Property="IsVisible" Value="True">
                                    <Setter Property="HorizontalContentAlignment"
                                            Value="{Binding Path=HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
                                    <Setter Property="VerticalContentAlignment"
                                            Value="{Binding Path=VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </ListBox.Resources>
                    <ListBox.ItemTemplate>
                          <DataTemplate>
                              <StackPanel Orientation="Horizontal" Margin="0,2,0,0">
                                  <TextBlock Text="{Binding Number}" />
                                  <StackPanel Orientation="Vertical" Margin="7,0,0,0">
                                      <TextBlock Text="{Binding File}" />
                                      <TextBlock Text="{Binding Dir}" Foreground="DarkGray" />
                                  </StackPanel>
                              </StackPanel>
                          </DataTemplate>
                      </ListBox.ItemTemplate>
                  </ListBox>
                  

                  这将在运行时在 VisualStudio 的 OutputWindow 中生成休闲线:

                  This will produce at runtime the fallowing Line in the OutputWindow of VisualStudio:

                  System.Windows.Data Error: 4 : 
                   Cannot find source for binding with reference 'RelativeSource FindAncestor, 
                   AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. 
                   BindingExpression:Path=HorizontalContentAlignment; DataItem=null; 
                   target element is 'ListBoxItem' (Name='');
                  

                  谁能给我一个提示,我该如何解决这个问题?

                  Can someone give me a tip, how I can solve this?

                  更新:

                  我已将属性添加到样式中以尝试消除警告/错误.

                  I have added the Properties to the style to try to eliminate the warning/error.

                  推荐答案

                  解决这个问题的最简单方法是确保你的 Listbox 有一个 ItemContainerStyle.请参阅以下示例:

                  The easiest way to solve this is to ensure that your Listbox has a ItemContainerStyle. See the following example:

                  <ListBox x:Name="RecentItemsListBox" Grid.Row="1" BorderThickness="0" Margin="2,0,0,0" SelectionChanged="RecentItemsListBox_SelectionChanged">
                      <ListBox.ItemContainerStyle>
                          <Style TargetType="{x:Type ListBoxItem}">
                              <Setter Property="HorizontalContentAlignment" Value="Left"/>
                              <Setter Property="VerticalContentAlignment" Value="Center"/>
                          </Style>
                      </ListBox.ItemContainerStyle>
                  
                  ...
                  
                  </ListBox>
                  

                  您的项目正在创建中,默认情况下它们会查找未定义的父级属性.明确定义它会解决这个问题.

                  What happens is that your Items are being created, and by default they look for parent's property which isn't defined. Explicitly defining it will solve this problem.

                  我在使用 TreeView 时遇到了同样的问题,更改这些模板的绑定源会导致这些警告.

                  I had the same issue using a TreeView and changing the bound source for these templates would cause those warnings.

                  这篇关于ListBoxItem 产生“System.Windows.Data 错误:4";绑定错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:C#:从列表中填充列表框的最简单方法 下一篇:绑定字典&lt;T&gt;到 WPF 列表框

                  相关文章

                  1. <legend id='6w6FN'><style id='6w6FN'><dir id='6w6FN'><q id='6w6FN'></q></dir></style></legend>
                    1. <small id='6w6FN'></small><noframes id='6w6FN'>

                      • <bdo id='6w6FN'></bdo><ul id='6w6FN'></ul>
                      <i id='6w6FN'><tr id='6w6FN'><dt id='6w6FN'><q id='6w6FN'><span id='6w6FN'><b id='6w6FN'><form id='6w6FN'><ins id='6w6FN'></ins><ul id='6w6FN'></ul><sub id='6w6FN'></sub></form><legend id='6w6FN'></legend><bdo id='6w6FN'><pre id='6w6FN'><center id='6w6FN'></center></pre></bdo></b><th id='6w6FN'></th></span></q></dt></tr></i><div id='6w6FN'><tfoot id='6w6FN'></tfoot><dl id='6w6FN'><fieldset id='6w6FN'></fieldset></dl></div>
                      <tfoot id='6w6FN'></tfoot>