在FlipView中的TextBox中切换TextWrapping

时间:2013-07-30 23:47:30

标签: c# .net xaml microsoft-metro windows-store-apps

我有一个FlipView,其中每个FlipViewItem包含一个绑定到ObservableCollection的TextBox,我需要为FlipView内的TextBox切换TextWrapping。

到目前为止,我已经尝试了所有我能想到的内容并且没有在线帮助。我找不到一个结果。

我该怎么做?

XAML:

...

// This part is for the AppBar Toggle button
<ToggleButton x:Name="wordWrapToggleButton" Style="{StaticResource WordWrapAppBarButtonStyle}" />

...

// For the FlipView
<FlipView x:Name="flipView" Grid.Row="1" Margin="0, 50, 0, 0" ItemsSource="{Binding Note, Mode=TwoWay}" Loaded="flipView_Loaded" SelectionChanged="flipView_SelectionChanged" FontSize="12.667">
    <FlipView.ItemTemplate>
        <DataTemplate>
            <TextBox Text="{Binding Contents, Mode=TwoWay}" Tag="{Binding Title, Mode=TwoWay}" TextWrapping="{Binding ElementName=wordWrapToggleButton, Path=.CheckState, Mode=TwoWay}" IsSpellCheckEnabled="True" AcceptsReturn="True" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderBrush="{x:Null}" FontSize="{Binding ElementName=flipView, Path=FontSize, Mode=OneWay}" />
        </DataTemplate>
    </FlipView.ItemTemplate>
</FlipView>

1 个答案:

答案 0 :(得分:1)

您必须创建一个binding converter,将bool转换为TextWrapping

public class BooleanToTextWrappingConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        return (value is bool && (bool)value) ? TextWrapping.Wrap : TextWrapping.NoWrap;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        return value is TextWrapping && (TextWrapping)value == TextWrapping.Wrap;
    }
}

并在绑定中使用它

<Page.Resources>
    <local:BooleanToTextWrappingConverter x:Key="BooleanToTextWrappingConverter"/>
</Page.Resources>
...
<DataTemplate>
    <TextBox Text="{Binding Contents, Mode=TwoWay}"
        TextWrapping="{Binding Path=IsChecked, ElementName=wordWrapToggleButton,
                       Converter={StaticResource BooleanToTextWrappingConverter}}"/>
</DataTemplate>

请注意TextWrapping绑定不是双向的,因为没有意义。