如何将TextBox的TextWrapping属性绑定到MenuItem的IsChecked值? [英] How do you bind the TextWrapping property of a TextBox to the IsChecked value of a MenuItem?

查看:210
本文介绍了如何将TextBox的TextWrapping属性绑定到MenuItem的IsChecked值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TextBox的TextWrapping属性有三个可能的值:




  • Wrap

  • NoWrap

  • WrapWithOverflow



我想绑定到MenuItem的IsChecked属性。如果选中了MenuItem,我想将TextBox的TextWrapping属性设置为Wrap。如果没有选中MenuItem,我想将TextBox的TextWrapping属性设置为NoWrap。



总之,我试图绑定一个控件,它有两个状态

如果可能的话,我想在XAML中实现这一点。



[edit] 我想出了如何使用IValueConverter做到这一点。也许有更好的方法来做到这一点?这是我做的:






在Window.Resources中,我声明了一个对我的ValueConverter的引用。

 < local:Boolean2TextWrapping x:Key =Boolean2TextWrapping/> 

在我的TextBox中,我创建了一个对MenuItem的绑定,并在绑定语句中包含了Converter。 / p>

  TextWrapping ={Binding ElementName = MenuItemWordWrap,Path = IsChecked,Converter = {StaticResource Boolean2TextWrapping}}

,ValueConverter看起来像这样:

  public class Boolean2TextWrapping:IValueConverter 
{
public object Convert(object value,Type targetType,object parameter,CultureInfo cultureInfo)
{
if((bool)value )== false)
{
return TextWrapping.NoWrap;
}
return TextWrapping.Wrap;
}

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


解决方案

您想要在xaml中执行此操作,您需要使用样式 DataTrigger 。 p>

 < StackPanel> 
< CheckBox x:Name =WordWrap> Word Wrap< / CheckBox>
< TextBlock Width =50>
Loreme ipsum dolor sit amet,consectetuer adipiscing elit。 Proin lacinia nibh non augue。 Pellentesque pretium neque et neque auctor adipiscing。

< TextBlock.Style>
< Style TargetType ={x:Type TextBlock}>
< Style.Triggers>
< DataTrigger Binding ={Binding IsChecked,ElementName = WordWrap}Value =True>
< Setter Property =TextWrappingValue =Wrap/>
< / DataTrigger>
< / Style.Triggers>
< / Style>
< /TextBlock.Style>
< / TextBlock>
< / StackPanel>


The TextWrapping property of the TextBox has three possible values:

  • Wrap
  • NoWrap
  • WrapWithOverflow

I would like to bind to the IsChecked property of a MenuItem. If the MenuItem is checked, I want to set the TextWrapping property of a TextBox to Wrap. If the MenuItem is not checked, I want to set the TextWrapping property of the TextBox to NoWrap.

To sum up, I am trying to bind a control that has two states to two values of an enumeration that has more than two values.

[edit] I would like to accomplish this in XAML, if possible.

[edit] I figured out how to do this using an IValueConverter. Perhaps there is a better way to do this? Here is what I did:


In Window.Resources, I declared a reference to my ValueConverter.

<local:Boolean2TextWrapping x:Key="Boolean2TextWrapping" />

In my TextBox, I created the binding to a MenuItem and included the Converter in the binding statement.

TextWrapping="{Binding ElementName=MenuItemWordWrap, Path=IsChecked, Converter={StaticResource Boolean2TextWrapping}}"

and the ValueConverter looks like this:

public class Boolean2TextWrapping : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo cultureInfo)
        {
            if (((bool)value) == false)
            {
                return TextWrapping.NoWrap;
            }
            return TextWrapping.Wrap;
        }

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

解决方案

If you want to do this all in xaml you need to use a Style and a DataTrigger.

<StackPanel>
    <CheckBox x:Name="WordWrap">Word Wrap</CheckBox>
    <TextBlock Width="50">
        Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Proin lacinia nibh non augue. Pellentesque pretium neque et neque auctor adipiscing.

        <TextBlock.Style>
            <Style TargetType="{x:Type TextBlock}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsChecked, ElementName=WordWrap}" Value="True">
                        <Setter Property="TextWrapping" Value="Wrap" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TextBlock.Style>
    </TextBlock>
</StackPanel>

这篇关于如何将TextBox的TextWrapping属性绑定到MenuItem的IsChecked值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆