【问题标题】:Toggle TextWrapping in TextBox inside FlipView在 FlipView 内的 TextBox 中切换 TextWrapping
【发布时间】:2013-07-30 23:47:29
【问题描述】:

我有一个 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>

【问题讨论】:

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


    【解决方案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 绑定不是双向的,因为这没有任何意义。

    【讨论】:

    • 我有一种感觉,那就是我需要做的,但有时解释我的想法只会让人们更加困惑。感谢您的帮助@Clemens :-)
    • 如果您不介意,我还有一个问题;您提供的代码给了我Error 1 The name "BooleanToTextWrappingConverter" does not exist in the namespace "using:myapp".Error 2 The type 'local:BooleanToTextWrappingConverter' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built...
    • Error 3 The type or namespace name 'BooleanToTextWrappingConverter' does not exist in the namespace 'myapp' (are you missing an assembly reference?)Error 4 The type 'myapp.MainPage' already contains a definition for 'BooleanToTextWrappingConverter'。知道这实际上意味着什么吗?我在网上搜索,但结果与不同的 IValueConverters 相关,我无法理解。
    • 在代码示例中,我假设您的 Page 中的 XAML xmlns:local=... 命名空间声明引用了 BooleanToTextWrappingConverter 类的命名空间。如果它在不同的命名空间中,则必须编写另一个 xmlns:... 声明。
    • 哦,对不起。我错误地将代码放在代码隐藏中的错误位置。现在效果很好,非常感谢您的帮助! :-)
    猜你喜欢
    • 2010-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-28
    • 1970-01-01
    相关资源
    最近更新 更多