停用ScatterView的自动SVI包装

时间:2014-03-12 16:27:39

标签: c# wpf mvvm pixelsense scatterview

我尝试将MVVM用于PixelSense项目。我将一些元素绑定到ScatterView:

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

我定义了一些DataTemplates:

    <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完成的。我现在的问题是:我如何停用此功能,或者您知道解决方法吗?

帮助我; - )

1 个答案:

答案 0 :(得分:0)

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

我从模板中删除了周围的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);
    }

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

相关问题