0

I have a FlipView where each FlipViewItem contains a TextBox bound to an ObservableCollection and I need to toggle TextWrapping for the TextBox's that are inside the FlipView.

I have tried everything I could think of and no help online thus far. Not a single result I could find.

How can I do this?

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>
4

1 回答 1

1

You have to create a binding converter that converts from bool to 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;
    }
}

and use that in your binding

<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>

Note that the TextWrapping binding isn't two-way, as that makes no sense.

于 2013-07-31T05:47:50.193 回答