C# 如何将用户控件中ListView列的显示成员绑定到自定义对象?

C# 如何将用户控件中ListView列的显示成员绑定到自定义对象?,c#,wpf,data-binding,dependency-properties,user-controls,C#,Wpf,Data Binding,Dependency Properties,User Controls,我有一个名为b_StringBoolTemplate的用户控件,它有一个ListView。ListView的项绑定到名为items的DependencyProperty。ListView有两列,一列用于文本框,另一列用于复选框。UserControl的XAML代码为: 在后面的代码中,我有一个名为b_StringBoolInfo的对象,它派生自ListViewItem,具有两个依赖属性,分别称为StringContent和BoolValue。我希望Textbox.Text绑定到StringC

我有一个名为b_StringBoolTemplate的用户控件,它有一个ListView。ListView的项绑定到名为items的DependencyProperty。ListView有两列,一列用于文本框,另一列用于复选框。UserControl的XAML代码为:


在后面的代码中,我有一个名为b_StringBoolInfo的对象,它派生自ListViewItem,具有两个依赖属性,分别称为StringContent和BoolValue。我希望Textbox.Text绑定到StringContent,Checkbox.IsChecked绑定到BoolValue,这样我就可以动态地将b_StringBoolInfo类型的项添加到UserControl

我也不确定我的方法是否是最好的方法。以防万一,将标题和项目资源绑定到Dendency道具非常重要

对不起,我有点含糊不清。提前感谢您的回复

编辑:对不起,我没有说清楚。 我已将类型为b_StringBoolInfo的项添加到窗口中的UserControl实例中。我正在尝试将TextBox.Text和Checkbox.IsChecked绑定到b_StringBool的属性,但不确定如何绑定

在代码中,我编写了一个名为b_StringBoolInfo的类,它源自ListViewItem:

public class b_StringBoolInfo :ListViewItem
    {


        public string StringContent
        {
            get { return (string)GetValue(StringContentProperty); }
            set { SetValue(StringContentProperty, value); }
        }

        // Using a DependencyProperty as the backing store for StringContent.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty StringContentProperty =
            DependencyProperty.Register("StringContent", typeof(string), typeof(b_StringBoolInfo),new PropertyMetadata("String"));



        public bool BoolValue
        {
            get { return (bool)GetValue(BoolValueProperty); }
            set { SetValue(BoolValueProperty, value); }
        }

        // Using a DependencyProperty as the backing store for BoolValue.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty BoolValueProperty =
            DependencyProperty.Register("BoolValue", typeof(bool), typeof(b_StringBoolInfo), new PropertyMetadata(true));



    }
我已经将用户控件添加到一个窗口中,即UserControl对象下面的xaml(称为b_StringBoolTemplate)及其项

<local:b_StringBoolTemplate Margin="64,67,159,145" StringLabel="Folder Location" BoolLabel="Search SubFolders?">
    <local:b_StringBoolTemplate.Items>

        <local:b_StringBoolInfo StringContent="String" BoolValue="True"/>

    </local:b_StringBoolTemplate.Items>
</local:b_StringBoolTemplate>

问题是以下行变白:

<TextBox Text="{Binding ElementName=StringBoolInfo, Path=Content}" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" />
告诉XAML解析器在XAML文件中查找名为b_StringBoolInfo的元素/控件

为了解决这个问题

将这些行更改为以下内容:

<TextBox Text="{Binding Content}" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" />



您还没有告诉我什么不起作用。很抱歉,我没有说清楚,所以我编辑了OP。我正在尝试将文本框的文本绑定到StringContent,并将复选框的复选框选中为BoolValue,这两个都是名为b_StringBoolInfo的自定义对象的属性。但无论我如何尝试绑定它们,它们都不起作用。我还尝试了“Text={Binding StringContent}”,但它不起作用。很抱歉造成混淆,b_StringBoolInfo是一个自定义对象而不是变量,我已将该类的代码添加到原始帖子中。我想将b_StringBool info类型的项添加到UserControl中,我正在尝试将TextBox.Text和Checkbox.IsChecked绑定到b_StringBool的属性,但不确定如何绑定。我添加了一个窗口的部分xaml代码,其中包含UserControl。
ElementName = b_StringBoolInfo
<TextBox Text="{Binding Content}" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" />
<CheckBox IsChecked="{Binding BoolValue}" />