【问题标题】:SurfaceListBox isSelected - System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestorSurfaceListBox isSelected - System.Windows.Data 错误:4:找不到与引用“RelativeSource FindAncestor”进行绑定的源
【发布时间】:2012-08-16 04:40:31
【问题描述】:

我有一个具有以下样式的表面列表框:

<s:SurfaceListBox.ItemContainerStyle>
    <Style TargetType="s:SurfaceListBoxItem">
        <Setter Property="HorizontalContentAlignment" Value="Center" />
        <Setter Property="HorizontalAlignment" Value="Center" />
        <Setter Property="FontSize" Value="13" />
        <Setter Property="Foreground" Value="Black" />
        <Setter Property="FontWeight" Value="Normal" />
        <Setter Property="RenderTransformOrigin" Value="0.5,0.5" />
        <Setter Property="MinHeight" Value="30" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type s:SurfaceListBoxItem}">
                    <ContentPresenter />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="HorizontalContentAlignment" Value="Center" />
                <Setter Property="HorizontalAlignment" Value="Center" />
                <Setter Property="FontWeight" Value="Bold"/>
                <Setter Property="Foreground" Value="#FF7E0123" />
                <Setter Property="RenderTransform">
                    <Setter.Value>
                        <ScaleTransform ScaleX="1.25" ScaleY="1.25"/>
                    </Setter.Value>
                </Setter>
            </Trigger>
        </Style.Triggers>
    </Style>
</s:SurfaceListBox.ItemContainerStyle>

我正在通过代码填充这个表面列表框,然后添加一个“显示全部”项:

private void AddShowAllCategories(int totalItems)
{
    SurfaceListBoxItem CategoriesListBoxItem = new SurfaceListBoxItem();
    CategoriesListBox.Items.Insert(0, CategoriesListBoxItem);
    CategoriesListBoxItem.Content = "Show All " + "(" + totalItems + ")";
    CategoriesListBoxItem.Margin = new Thickness(0,30,0,0);
    CategoriesListBoxItem.IsSelected = true;  //This is what is causing the issue
}

一切正常,在我通过代码将IsSelected 设置为true 之前,我没有收到任何错误。然后我得到这个错误:

System.Windows.Data 错误:4:找不到与引用'RelativeSource FindAncestor,AncestorType='System.Windows.Controls.ItemsControl',AncestorLevel='1''的绑定源。 BindingExpression:Path=Horizo​​ntalContentAlignment;数据项=空;目标元素是'SurfaceListBoxItem'(名称='');目标属性是“Horizo​​ntalContentAlignment”(类型“Horizo​​ntalAlignment”)

如果我不通过代码将IsSelected 设置为true,而是单击一个项目,一切都很好。有什么想法吗?

【问题讨论】:

    标签: wpf pixelsense


    【解决方案1】:

    由于您没有提供使用代码选择项目的代码,我不知道您是否使用 MVVM、使用代码隐藏或您是否已经尝试过以下方法。

    我遵循了您的方案并设法使用“ItemContainerGenerator”从代码中选择了一个项目:

            // Selecting an item given the actual item
            object requestedItem = null; // TODO - You should set here the actual item
            SurfaceListBoxItem itemContainer = itemsList.ItemContainerGenerator.ContainerFromItem(requestedItem) as SurfaceListBoxItem;
            itemContainer.IsSelected = true;
    
            // Selecting an item given his index in the item source
            SurfaceListBoxItem itemContainer2 = itemsList.ItemContainerGenerator.ContainerFromIndex(2) as SurfaceListBoxItem;
            itemContainer2.IsSelected = true;
    

    如果您对要选择的项目有引用,请使用第一种方法,如果您只知道提供的列表中的索引,请使用第二种方法。

    让我知道它是否对你有用。

    伊兰。

    【讨论】:

    • 我添加了导致问题的代码。当我设置 CategoriesListBoxItem.IsSelected = true 时一切正常,但在输出窗口中出现上述错误
    猜你喜欢
    • 1970-01-01
    • 2016-04-08
    • 2018-09-01
    • 1970-01-01
    • 2011-04-23
    • 2013-03-07
    • 2011-01-18
    • 2011-02-06
    • 2011-03-12
    相关资源
    最近更新 更多