0

我试图让 aListBox将其背景从 a 更改为单击 a 时Grid选择的用户。除了背景之外,列表框还包含一组 UI 元素,这些元素绑定到via ,然后通过 item Presenter 呈现。ImageButtonObservableCollectionItemSource

最初我在ListBox's 的模板中有背景 XML,这一直很好,直到我需要在其中放置一个内容演示者,让用户可以选择不同的背景。

背景更新很好,只是没有ListBox显示任何项目。知道为什么会这样吗?这是代码:

<ListBox SelectedItem="{Binding SelectedObject}" 
                 PreviewMouseMove="ListBox_PreviewMouseMove"
                 PreviewMouseDown="ListBox_PreviewMouseDown">
                <ListBox.Template>
                    <ControlTemplate>
                        <!--<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" Content="{Binding DataContext.BackgroundType, Source={x:Reference view}}">-->
                        <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
                            <Border>
                                <ContentPresenter Content="{Binding DataContext.BackgroundType, Source={x:Reference view}}">
                                    <ContentPresenter.Resources>
                                        <DataTemplate DataType="{x:Type local:GridBackType}">
                                            <Border>
                                                <Border.Background>
                                                <VisualBrush TileMode="Tile" Viewport="{Binding DataContext.GridSize, Source={x:Reference view}}" 
                                                             ViewportUnits="Absolute" Viewbox="0,0,50,50" ViewboxUnits="Absolute">
                                                    <VisualBrush.Visual>
                                                        <Rectangle Stroke="Blue" StrokeThickness="1" Height="50" Width="50"
                                                                   StrokeDashArray="5 3">
                                                        </Rectangle>
                                                    </VisualBrush.Visual>
                                                </VisualBrush>
                                            </Border.Background>
                                                <ItemsPresenter/>
                                            </Border>
                                        </DataTemplate>
                                    </ContentPresenter.Resources>
                                </ContentPresenter>
                            </Border>
                        </ScrollViewer>
                    </ControlTemplate>
                </ListBox.Template>
                <ListBox.ItemsSource>
                    <StaticResource ResourceKey="Col"/>
                </ListBox.ItemsSource>
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <Canvas IsItemsHost="True" Background="#01FFFFFF" 
                            Height="{Binding AreaHeight}" Width="{Binding AreaWidth}"
                            VerticalAlignment="Top" HorizontalAlignment="Left"/>
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
                <ListBox.ItemContainerStyle>
                    <Style TargetType="ListBoxItem">
                        <Setter Property="Canvas.Left" Value="{Binding X}"/>
                        <Setter Property="Canvas.Top" Value="{Binding Y}"/>
                        <Setter Property="FocusVisualStyle" Value="{StaticResource EmptyFocusVisualStyle}"/>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="ListBoxItem">
                                    <ContentPresenter x:Name="Content"/>
                                    <ControlTemplate.Triggers>
                                        <Trigger Property="IsSelected" Value="True">
                                            <Setter TargetName="Content" Property="Effect">
                                                <Setter.Value>
                                                    <DropShadowEffect Color="Gray" ShadowDepth="4" BlurRadius="10"/>
                                                </Setter.Value>
                                            </Setter>
                                        </Trigger>
                                        <DataTrigger Binding="{Binding IsNew}" Value="True">
                                            <Setter Property="Opacity" Value=".5"/>
                                        </DataTrigger>
                                    </ControlTemplate.Triggers>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </ListBox.ItemContainerStyle>
            </ListBox>

BackgroundType 类的代码:

namespace NodesEditor
{
public abstract class BackgroundTypes : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

public class ImageBackType : BackgroundTypes { }
public class GridBackType : BackgroundTypes { }
}
4

1 回答 1

2

您可以使用 aGrid放置ItemsPresenter“背景”面板的“上方”(在 Z-Index 中),从而ItemsPresenter在能够修改背景内容的同时始终保持原位:

<Grid>
    <ContentPresenter Content="{Binding DataContext.BackgroundType, Source={x:Reference view}}">
        <ContentPresenter.Resources>
            <!-- etc -->
        </ContentPresenter.Resources>
    </ContentPresenter>
    <ItemsPresenter/>
</Grid>
于 2013-05-01T16:18:58.843 回答