禁用ScatterView的自动svi包装

本文关键字:svi 包装 ScatterView 禁用 | 更新日期: 2023-09-27 17:50:04

我尝试在pixelsense项目中使用MVVM。我将一些元素绑定到ScatterView:

<s:ScatterView x:Name="MainScatterView" ItemTemplateSelector="{DynamicResource myDataTemplateSelector}" ItemsSource="{Binding Path=MainMenus}"/>

我定义了一些datatemplate:

    <DataTemplate x:Key="ActivityTemplate">
        <s:ScatterViewItem Loaded="ScatterViewItem_Loaded">
            <TextBlock Text="{Binding Path=Text}" />
        </s:ScatterViewItem>
    </DataTemplate>
    <DataTemplate x:Key="MainMenuTemplate">
        <s:ScatterViewItem Height="{Binding Path=Size, Mode=TwoWay}" Width="{Binding Path=Size, Mode=TwoWay}">
            <TextBlock/>
        </s:ScatterViewItem>
    </DataTemplate>

如你所见,我尝试绑定(例如)height属性到ViewModel。

它不起作用,因为我的SVI (ScatterViewItem)将自动被另一个SVI包装。这是由ScatterView完成的。我现在的问题是:我怎么才能停用这个,或者你知道一个解决方案吗?

-)

禁用ScatterView的自动svi包装

我找到了一个解决方法…这不是最好的,但它有效:)也许有人也会有这个问题:

我从模板中删除了周围的ScatterViewItem,并添加了Loaded-event:

    <DataTemplate x:Key="ActivityTemplate">
            <TextBlock Text="{Binding Path=Text}" Loaded="TextBlock_Loaded"/>
    </DataTemplate>
    <DataTemplate x:Key="MainMenuTemplate">
            <TextBlock Width="20" Height="20" Text="Hallo" Loaded="TextBlock_Loaded"/>
    </DataTemplate>

其余部分在后面的代码中:

    private void TextBlock_Loaded(object sender, RoutedEventArgs e)
    {
        //Get the sourrounding ScatterViewItem via the VisualTree
        System.Windows.Media.Visual parent = (System.Windows.Media.Visual)VisualTreeHelper.GetParent((System.Windows.Media.Visual)sender);
        while (!(parent is ScatterViewItem))
        {
            parent = (System.Windows.Media.Visual)VisualTreeHelper.GetParent((System.Windows.Media.Visual)parent);
        }
        //the current parent is the surrounding SVI
        ScatterViewItem svi = parent as ScatterViewItem;
        //Bind the properties to the SVI
        Binding myBinding = new Binding("Size");
        myBinding.Source = svi.DataContext;
        svi.SetBinding(ScatterViewItem.HeightProperty, myBinding);
        svi.SetBinding(ScatterViewItem.WidthProperty, myBinding);
    }

如果你知道一个更好的解决方案,请告诉我;)