【问题标题】:ListBox Bound to SortedSet. How To Bind TextBox?ListBox 绑定到 SortedSet。如何绑定文本框?
【发布时间】:2012-03-14 18:59:25
【问题描述】:

ItemsSource (SsString) 是一个 SortedSet 字符串

我想在 ListBox 中使用 TextBox(不是默认的 TextBlock),但我不知道如何绑定到 SortSet 中的值。

我尝试过没有路径、Key、key、Value 和 value 的绑定。

    <ListBox ItemsSource="{Binding Path=SsString}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBox Text="{Binding Path=Key}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

以下工作,但它显示 TextBlock。

   <ListBox ItemsSource="{Binding Path=SsString}" />

【问题讨论】:

    标签: wpf xaml data-binding textbox listbox


    【解决方案1】:

    您将无法编辑绑定值。您需要一个类作为项目,以便您可以使用 Binding.Path 定位属性。

    (您应该可以使用{Binding .} 绑定它,但它是单向的)

    【讨论】:

    • 谢谢,加 1 在我知道我有第二个问题之前就知道答案 - 如何双向绑定?
    • @Blam:对于TextBox.Text 默认绑定TwoWay(请参阅dependency property - metadata properties)如果您有路径,如果目标属性没有设置器,则会抛出错误。
    • 我希望用户能够编辑列表中的字符串项目,但我需要强制执行唯一性。如果他们输入 null 以删除该项目。我想我需要使用 ObservableCollection 并将集合传递给 Item 以便项目可以检查唯一性
    • @Blam:这听起来很混乱,可能更容易验证整个集合,而不是在项目级别进行。
    • OK 那怎么办?在项目级别,我需要允许更新(并持续到 SQL)。
    猜你喜欢
    • 1970-01-01
    • 2023-03-13
    • 2011-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-15
    • 1970-01-01
    • 2015-01-04
    相关资源
    最近更新 更多