你如何绑定一个TextBox到一个菜单项的值器isChecked的TextWrapping的财产? [英] How do you bind the TextWrapping property of a TextBox to the IsChecked value of a MenuItem?
问题描述
TextBox的TextWrapping属性有三种可能的值:
- 裹
- NoWrap的
- WrapWithOverflow
我想绑定到一个菜单项的财产器isChecked。如果菜单项被选中,我想设置一个文本框的TextWrapping属性为Wrap。如果未选中的菜单项,我想TextBox的TextWrapping属性设置为NoWrap的。
要总结,我想绑定有两州有两个以上的值的枚举的两个值的控制。
我想在XAML中,如果有可能做到这一点。
我想出如何使用的IValueConverter做到这一点。或许有更好的方式来做到这一点?下面是我所做的:
在Window.Resources,我宣布我的ValueConverter参考。
<局部:Boolean2TextWrapping X:键=Boolean2TextWrapping/>
在我的文本框,我创建了绑定到一个菜单项,其中包括在绑定语句中的转换器。
TextWrapping ={绑定的ElementName = MenuItemWordWrap,路径=器isChecked,转换器= {StaticResource的Boolean2TextWrapping}}
和ValueConverter看起来是这样的:
公共类Boolean2TextWrapping:的IValueConverter
{
公共对象转换(对象的值,类型TARGETTYPE,对象参数,CultureInfo的CultureInfo的)
{
如果(((布尔)值)== FALSE)
{
返回TextWrapping.NoWrap;
}
返回TextWrapping.Wrap;
} 公共对象ConvertBack(对象的值,类型TARGETTYPE,对象参数,System.Globalization.CultureInfo文化)
{
抛出新NotImplementedException();
}
}
如果你想要做的这一切都在XAML中,你需要使用的风格和 DataTrigger
<&StackPanel的GT;
<复选框X:NAME =自动换行>自动换行< /复选框>
< TextBlock的宽度=50>
Lorem存有悲坐阿梅德,consectetuer adipiscing ELIT。 PROIN lacinia NIBH非augue。 Pellentesque pretium预期值,也预期值,也等auctor adipiscing。 < TextBlock.Style>
<风格的TargetType ={X:类型的TextBlock}>
< Style.Triggers>
< DataTrigger绑定={结合器isChecked,的ElementName =自动换行}VALUE =真>
< setter属性=TextWrappingVALUE =自动换行/>
< / DataTrigger>
< /Style.Triggers>
< /样式和GT;
< /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到一个菜单项的值器isChecked的TextWrapping的财产?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!