English 中文(简体)
运行时隐藏和显示数据匹配的Pivit项目页眉
原标题:Hiding and showing a databound PivotItem header during runtime

我认为目前PivotItem的实施有些前后矛盾,不能说有缺陷。 如果产权信头在完全设定之前或之后被设定,则标题的大小被适当设定。 但是, 将信头重新设置为空或字符串并不导致标题再次缩小。 当信头通过 DataBinding 设定时, 是否有一种优雅的隐藏和显示 PivotItem 标题的方法, 或者能否强迫一个 PivotItem 重新确认其边界?

从现在开始,我试图用一些相当粗糙的代码来“解决”我的问题,它包括事件和完全的Pivot 程序娱乐,然后再次附上所有的Pivot Projects。 也许对正页来说是一个有效的解决方案,但是如果这需要10倍以上的时间来完成的话,肯定有些不切实际。

[这个问题在 前被问到 。]

最佳回答

我发现,改变“维瓦特项目”的常识或降低“维瓦特项目”的顶部高度对已经完成的项目根本行不通(还不是吗? ) 。 它只是破碎了, 但可以通过将“维瓦特项目”的第一个孩子元素移到负值, 从而绕开这些缺陷。 诚然, 它不是优雅的,而是起作用的。 下面的代码是:

<phone:PhoneApplicationPage.Resources>
    <local:SubtitleConverter x:Key="SubtitleConverter" />
    <local:MarginConverter x:Key="MarginConverter" />       
</phone:PhoneApplicationPage.Resources>

<controls:PivotItem Name="LayoutItem" Header="{Binding Path=MyProgramSettings.ShowHeader, Converter={StaticResource SubtitleConverter}}">
    <ListBox Margin="{Binding Path=MyProgramSettings.ShowHeader, Converter={StaticResource MarginConverter}}"> 
        <!-- element that is moved up and down depending on some property -->
    </ListBox>
</controls:PivotItem>

后面的守则

public class SubtitleConverter : ConverterBase
    {
        public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {   
            /* should always return a non empty string, making the PivotItem header height predictable */
            /* needed for MarginConverter */
            return MyProgramSettings.ShowHeader ? " " : MyResource.SomeText.ToLower();
        }
    }

    public class MarginConverter : ConverterBase
    {
        public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is bool && (bool)value)
            {
                return new Thickness(12, 0, -28, 0);
            }
            return new Thickness(12, 0, -12, 0);
        }
    }
问题回答

您试图删除仅一个条目的页眉而保留其他页眉吗? 如果是的话, 我建议不要这样做。 这将非常混乱 。

如果试图将其全部全部拆掉, 您可以将Pivot Header Template 的可见度绑在几件事情上。 一个是您页面的数据链接 。

<controls:Pivot x:Name="MyPivot" >
    <controls:Pivot.HeaderTemplate>
        <DataTemplate>
            <Grid Visibility="{Binding DataContext.HeaderVisibility, ElementName=MyPivot}">
            <TextBlock Text="{Binding}" FontSize="{StaticResource PhoneFontSizeExtraExtraLarge}" />
            </Grid>
        </DataTemplate>
    </controls:Pivot.HeaderTemplate>

然后您可以在页面的视图模型中 拥有一个“页眉可视性”属性。

如果您想要将文本设置为页眉的空, 您可以使用一个值转换器

xaml :

<controls:Pivot x:Name="MyPivot" >
    <controls:Pivot.HeaderTemplate>
        <DataTemplate>
            <Grid Visibility="{Binding Converter={StaticResource TextToVisibilityConverter}}">
            <TextBlock Text="{Binding}" FontSize="{StaticResource PhoneFontSizeExtraExtraLarge}"/>
            </Grid>
        </DataTemplate>
    </controls:Pivot.HeaderTemplate>

和转换器:

public class TextToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null) return Visibility.Collapsed;

        return (string.InNullOrEmpty(value.ToString()) ? Visibility.Collapsed : Visibility.Visible;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

您是否也尝试过将正轴页眉高度绑绑到一个属性上, 该属性会更新, 因为页眉文字设置为空/ null( 值 0? )

如果这样有效,也许你可以尝试的另外一件事是,将高度属性与页眉文本的值绑在一起,加上一个转换器(假:如果文本为无效,则返回 0 高度)-这样你就不会有额外的属性值得担心。

Great idea! I don t use your solution exact, but I was inspired by your idea.

这就是我所做的:

1. 将堆叠板建成集装箱,设定负值的顶部差值,例如 Margin="0,-35,0,0"

2. 添加几个文本块以伪造紫外页头, 设定您打算与页眉捆绑的数据绑定 。

3. Set TextBlocks < seet TextBlocks>code> Width = "auto" ,加上一些右边边边距,以免过于拥挤

注:当第一个TextBlock代表当前的PivotTroject时,其他代表其他 -- -- 即未激活的 -- -- 。

4. 对于活性活性聚变项的文本锁,我们设置了 Foreground="#FF 7272772" 来与激活的区分。同时添加一个事件处理器,例如 Tap="GoToPivit2_Tap" ,其后面有简单的代码: PivotRooot.Sectroduced Text=Pivot2;

<controls:Pivot x:Name="PivotRoot">

    <controls:PivotItem x:Name="Pivot1">
        <StackPanel Margin="0,-35,0,0" Orientation="Horizontal">
            <TextBlock Text="{Binding Header1}" Width="Auto" Margin="0,0,25,0" FontSize="30"/>
            <TextBlock Text="{Binding Header2}" Tap="GoToPivot2_Tap" Width="Auto" Margin="0,0,25,0" Foreground="#FF727272" FontSize="30"/>
            <TextBlock Text="{Binding Header3}" Tap="GoToPivot3_Tap" Width="Auto" Margin="0,0,25,0" Foreground="#FF727272" FontSize="30"/>
        </StackPanel>
    </controls:PivotItem>

    <controls:PivotItem x:Name="Pivot2">
        <StackPanel Margin="0,-35,0,0" Orientation="Horizontal">
            <TextBlock Text="{Binding Header2}" Width="Auto" Margin="0,0,25,0" FontSize="30"/>
            <TextBlock Text="{Binding Header3}" Tap="GoToPivot3_Tap" Width="Auto" Margin="0,0,25,0" Foreground="#FF727272" FontSize="30"/>
            <TextBlock Text="{Binding Header1}" Tap="GoToPivot1_Tap" Width="Auto" Margin="0,0,25,0" Foreground="#FF727272" FontSize="30"/>
        </StackPanel>
    </controls:PivotItem>

    <controls:PivotItem x:Name="Pivot3">
        <StackPanel Margin="0,-35,0,0" Orientation="Horizontal">
            <TextBlock Text="{Binding Header3}" Width="Auto" Margin="0,0,25,0" FontSize="30"/>
            <TextBlock Text="{Binding Header1}" Tap="GoToPivot1_Tap" Width="Auto" Margin="0,0,25,0" Foreground="#FF727272" FontSize="30"/>
            <TextBlock Text="{Binding Header2}" Tap="GoToPivot2_Tap" Width="Auto" Margin="0,0,25,0" Foreground="#FF727272" FontSize="30"/>
        </StackPanel>
    </controls:PivotItem>
</controls:Pivot>

就是这样。

事件可能不如你那么优雅,但也有效。





相关问题
WPF convert 2d mouse click into 3d space

I have several geometry meshes in my Viewport3D, these have bounds of (w:1800, h:500, d:25). When a user clicks in the middle of the mesh, I want the Point3D of (900, 500, 25)... How can I achieve ...

Editing a xaml icons or images

Is it possible to edit a xaml icons or images in the expression design or using other tools? Is it possible to import a xaml images (that e.g you have exported) in the expression designer for editing?...

WPF: writing smoke tests using ViewModels

I am considering to write smoke tests for our WPF application. The question that I am faced is: should we use UI automation( or some other technology that creates a UI script), or is it good enough to ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

How do WPF Markup Extensions raise compile errors?

Certain markup extensions raise compile errors. For example StaticExtension (x:Static) raises a compile error if the referenced class cannot be found. Anyone know the mechanism for this? Is it baked ...

WPF design-time context menu

I am trying to create a custom wpf control, I m wondering how I can add some design-time features. I ve googled and can t seem to get to my goal. So here s my simple question, how can I add an entry ...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签