【问题标题】:ListBox not displaying string (but bools) using databinding in WPF列表框不显示字符串(但布尔值)在 WPF 中使用数据绑定
【发布时间】:2014-02-12 08:10:48
【问题描述】:

我想在列表框中显示一个字符串和三个布尔值,因此我制作了一个 ItemTemplate:

    <ListBox x:Name="lstVars" Margin="10,41" Grid.ColumnSpan="4" Grid.Column="1">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <WrapPanel Height="30">
                    <TextBlock  Text="{Binding description}" VerticalAlignment="Center" HorizontalAlignment="Left" Width="200"/>
                    <CheckBox Margin="3" Content="a" IsChecked="{Binding save}" Width="200"/>
                    <CheckBox Margin="3" Content="b" IsChecked="{Binding displayBoard}"  Width="200"/>
                    <CheckBox Margin="3" Content="c" IsChecked="{Binding displayGraph}" Width="200"/>
                </WrapPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

我做了一个测试类,因为我原来的类它不起作用(但同样的“错误”发生在测试类中):

public class test
{
    public String description;
    public bool save { get; set; }
    public bool displayBoard { get; set; }
    public bool displayGraph { get; set; }

    public test(String description, bool save, bool displayBoard, bool displayGraph)
    {
        this.description = description;
        this.save = save;
        this.displayBoard = displayBoard;
        this.displayGraph = displayGraph;
    }
}

当我向列表框添加一些值时,字符串不显示

lstVars.Items.Add(new test("teststring", true, false, true));

我最初以为文本只是在不可见的行中,但是当我写 Text="123test" 而不是 Text="{Binding description}" 时,它会显示 123test 应该的样子。

【问题讨论】:

    标签: c# wpf data-binding listbox


    【解决方案1】:

    您必须将绑定对象声明为属性。就像你的布尔属性一样。

    public String description {get; set;}
    

    【讨论】:

    • 非常感谢您!我从未使用过数据绑定,我几乎知道我犯了一个愚蠢的小错误:-D
    • 这确实是一个小错误,但会引起很多头痛:)
    猜你喜欢
    • 2014-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-07
    • 2019-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多