0

我有一个ScrollViewerwhichWidth属性绑定到另一个页面元素。我只想将此应用于某些AdaptiveTrigger MinWindowWidth

<ScrollViewer x:Name="scrollViewerEditor" Width="{Binding ElementName=stackPanelSearch, Path=ActualWidth}">
    ... 
</ScrollViewer>

我想像下面这样放置Width绑定AdaptiveTrigger。正确的方法是什么?

<VisualState x:Name="VisualStateWide">
    <VisualState.StateTriggers>
        <AdaptiveTrigger MinWindowWidth="{StaticResource WideMinWidth}" />
    </VisualState.StateTriggers>
    <VisualState.Setters>
        <Setter Target="scrollViewerEditor.(FrameworkElement.Width)" Value="Binding ElementName=stackPanelSearch, Path=ActualWidth}" />
4

1 回答 1

0

如果您不介意在后面的代码中完成这项工作,有一种简单的方法可以解决这个问题,例如使用VisualStateGroup.CurrentStateChanged 事件,如下所示:

<VisualStateManager.VisualStateGroups>
    <VisualStateGroup CurrentStateChanged="VisualStateGroup_CurrentStateChanged">
        <VisualState x:Name="VisualStateNarrow">
            <VisualState.StateTriggers>
                <AdaptiveTrigger MinWindowWidth="0" />
            </VisualState.StateTriggers>
            <VisualState.Setters>
                <Setter Target="scrollViewerEditor.Style" Value="{StaticResource ScrollViewerStyle1}" />
            </VisualState.Setters>
        </VisualState>
        <VisualState x:Name="VisualStateWide">
            <VisualState.StateTriggers>
                <AdaptiveTrigger MinWindowWidth="720" />
            </VisualState.StateTriggers>
            <VisualState.Setters>
                <Setter Target="scrollViewerEditor.Style" Value="{StaticResource ScrollViewerStyle2}" />
            </VisualState.Setters>
        </VisualState>
    </VisualStateGroup>
</VisualStateManager.VisualStateGroups>

在后面的代码中:

private void VisualStateGroup_CurrentStateChanged(object sender, VisualStateChangedEventArgs e)
{
    var state = e.NewState;
    if (state.Name == "VisualStateNarrow")
    {
        scrollViewerEditor.Width = 300;
    }
    else
    {
        if (tb.ActualWidth != 0)
            scrollViewerEditor.Width = tb.ActualWidth;
        else
            scrollViewerEditor.Width = 600;
    }
}

中不允许使用数据绑定Setter,通常解决这个问题的正确方法是AttachedProperty为你创建一个自定义ScrollViewer并将这个附加属性绑定到 中WidthProperty,但是对自定义附加属性的支持VisualState不太理想,我会保留对此进行研究,看看它是否适用于 custom AttachedProperty

于 2016-09-26T11:05:44.983 回答