如何将 TextBox 的 TextWrapping 属性绑定到 MenuItem 的 IsChecked 值?
TextBox 的 TextWrapping 属性具有三个可能的值:
- Wrap
- NoWrap
- WrapWithOverflow
我想绑定到 MenuItem 的 IsChecked 属性。 如果选中 MenuItem,我想将 TextBox 的 TextWrapping 属性设置为 Wrap。 如果未选中 MenuItem,我想将 TextBox 的 TextWrapping 属性设置为 NoWrap。
总而言之,我试图将具有两种状态的控件绑定到具有两个以上值的枚举的两个值。
[编辑] 如果可能的话,我想在 XAML 中完成此操作。
[编辑] 我想出了如何使用 IValueConverter 来做到这一点。 也许有更好的方法来做到这一点? 这是我所做的:
在 Window.Resources 中,我声明了对 ValueConverter 的引用。
<local:Boolean2TextWrapping x:Key="Boolean2TextWrapping" />
在我的 TextBox 中,我创建了到 MenuItem 的绑定,并将 Converter 包含在绑定语句中。
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();
}
}
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果你想在 xaml 中完成这一切,你需要使用 Style 和 DataTrigger。
If you want to do this all in xaml you need to use a Style and a DataTrigger.
我认为
唯一且正确的执行此操作的典型方法是使用您已经做过的值转换器。有时,您可以找到您已经构建的现有价值转换器......甚至是 Microsoft 为您构建的更好的转换器。 例如,在 System.Windows.Controls 中,Microsoft 编写了一个 BooleanToVisibilityConverter ...它将 bool 转换为 Visibility 枚举...将 True 转换为 Visible,将 False 转换为 Collapsed(并且不用担心隐藏)。
一种想法是使用 .NET Reflector,导航到 System.Windows.Data.IValueConverter,然后使用分析功能(特别是“使用者”)并查看哪些东西实现了 IValueConverter ...您可能会得到很幸运找到适合您用途的转换器。
在相关说明中,BooleanToVisibilityConverter 与您上面尝试执行的操作非常相似。
编辑:
我真的很喜欢 Todd White 关于设置 TextBox 样式并在 Style 中使用 DataTrigger 的建议。 如果您想避免使用转换器,这是一个非常好的主意。
I think that
the only and rightthe typical way to do this is to use a value converter like you already have done.Sometimes you can find an existing value converter that you have built already ... or even better that Microsoft has built for you. For example, in System.Windows.Controls, Microsoft has written a BooleanToVisibilityConverter ... which converts a bool into a Visibility enum ... converting True to Visible and False to Collapsed (and not worrying about Hidden).
One idea is to use .NET Reflector, navigate to the System.Windows.Data.IValueConverter, and then use the Analyze feature (in particular, 'Used by') and see what things have implemented IValueConverter ... and you just might get lucky to find a converter that suits your purpose.
On a related note, BooleanToVisibilityConverter is very similar to what you are trying to do above.
Edit:
I really like Todd White's suggestion of styling the TextBox and using a DataTrigger in the Style. It is a very good idea if you want to avoid a Converter.
我假设您正在谈论 .NET。 我认为数据绑定在这里不起作用,因为这些值不是同一类型(布尔值与枚举)。 最简单的解决方案是处理该菜单项的 CheckedChanged 事件并相应地调整文本框的换行模式。
I assume you are talking about .NET. I don't think databinding will work here because the values are not of the same type (boolean vs enum). The easiest solution would be to handle the CheckedChanged event of that menu item and adjust the wrap mode of the textbox accordingly.