小编Tim*_*ell的帖子

如何将TextBox的TextWrapping属性绑定到MenuItem的IsChecked值?

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

  • 包裹
  • NoWrap的
  • WrapWithOverflow

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

总而言之,我试图将具有两个状态的控件绑定到具有两个以上值的枚举的两个值.

[edit]我想在XAML中完成这个,如果可能的话.

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


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

<local:Boolean2TextWrapping x:Key="Boolean2TextWrapping" />
Run Code Online (Sandbox Code Playgroud)

在我的TextBox中,我创建了与MenuItem的绑定,并在绑定语句中包含了Converter.

TextWrapping="{Binding ElementName=MenuItemWordWrap, Path=IsChecked, Converter={StaticResource Boolean2TextWrapping}}"
Run Code Online (Sandbox Code Playgroud)

而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();
        }
    }
Run Code Online (Sandbox Code Playgroud)

wpf checkbox enums binding word-wrap

5
推荐指数
1
解决办法
3036
查看次数

将Xml数据双向绑定到WPF TreeView

我正在尝试使用WPF为表示层重写我的ForestPad应用程序.在WinForms中,我以编程方式填充每个节点,但如果可能的话,我想利用WPF的数据绑定功能.

一般来说,将WPF TreeView双向数据绑定到Xml文档的最佳方法是什么?

一般的解决方案很好,但作为参考,我试图绑定的Xml文档的结构如下所示:

<?xml version="1.0" encoding="utf-8"?>
<forestPad
    guid="6c9325de-dfbe-4878-9d91-1a9f1a7696b0"
    created="5/14/2004 1:05:10 AM"
    updated="5/14/2004 1:07:41 AM">
<forest 
    name="A forest node"
    guid="b441a196-7468-47c8-a010-7ff83429a37b"
    created="01/01/2003 1:00:00 AM"
    updated="5/14/2004 1:06:15 AM">
    <data>
    <![CDATA[A forest node
        This is the text of the forest node.]]>
    </data>
    <tree
        name="A tree node"
        guid="768eae66-e9df-4999-b950-01fa9be1a5cf"
        created="5/14/2004 1:05:38 AM"
        updated="5/14/2004 1:06:11 AM">
        <data>
        <![CDATA[A tree node
            This is the text of the tree node.]]>
        </data>
        <branch
            name="A branch node"
            guid="be4b0993-d4e4-4249-8aa5-fa9c940ae2be"
            created="5/14/2004 1:06:00 AM"
            updated="5/14/2004 1:06:24 AM">
            <data>
            <![CDATA[A branch node …
Run Code Online (Sandbox Code Playgroud)

c# xml data-binding wpf treeview

4
推荐指数
1
解决办法
2万
查看次数

标签 统计

wpf ×2

binding ×1

c# ×1

checkbox ×1

data-binding ×1

enums ×1

treeview ×1

word-wrap ×1

xml ×1