Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将代码隐藏中的值绑定到wpf中的datatemplate控件_C#_Wpf - Fatal编程技术网

C# 将代码隐藏中的值绑定到wpf中的datatemplate控件

C# 将代码隐藏中的值绑定到wpf中的datatemplate控件,c#,wpf,C#,Wpf,我是wpf新手,我创建了一个listbox,它将创建一个动态listitems,这里我使用datetemplate,它包含两个控件,即两个textblocks,一个textblocks包含绑定值表单combobox(即字符串数据类型),另一个是绑定代码绑定的值 XAML 在此类中,为hourvalue创建属性。对于这个类,我在下面提到的codebehind文件中创建了一个对象。创建一个对象并为hourvalue变量赋值 C#(代码隐藏) 在这里,我为Rounderhour类创建了一个对象。将值指

我是wpf新手,我创建了一个listbox,它将创建一个动态listitems,这里我使用datetemplate,它包含两个控件,即两个textblocks,一个textblocks包含绑定值表单combobox(即字符串数据类型),另一个是绑定代码绑定的值

XAML

在此类中,为hourvalue创建属性。对于这个类,我在下面提到的codebehind文件中创建了一个对象。创建一个对象并为hourvalue变量赋值

C#(代码隐藏)

在这里,我为Rounderhour类创建了一个对象。将值指定给该属性的小时值。但我无法将codebind中的值绑定到XAML页面。

使用绑定变量分配XAML对象的“ItemsSource”属性

另外,将对象本身绑定到对象的属性(如

this.DataTemplate = this;
使用:

List bindingObjectList=new List();
//将对象插入列表中
this.ItemsSource=bindingObjectList;
在这里,您可以找到一个示例:


您的
项目资源应为
集合类型

ListBox ItemsSource="{Binding obj}" 
您还应该开始为变量和属性指定有意义的名称。这使得以后阅读代码更加容易on

第二个问题是
绑定本身

您是这样绑定的:
Text=“{binding roundedhourvalue}

因此WPF在
obj
上需要一个属性
roundedhourvalue

但如您的
codebeard
中所示,只有
obj.hourvalue

因此,将绑定更改为
Text=“{Binding hourvalue}

查看您的
输出窗口
了解详细信息。

注意:

string roundedhourvalue = obj.hourvalue;
没有
getter
,并且由于其
私有
而不可访问

注意:您可以使用
绑定
或在CodeBehind中设置项目资源


试着这样做:

只需删除所有格式和内容:

<ListBox ItemsSource="{Binding RoundedHours}" x:Name="ListBox">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" Margin="5" >
               <TextBlock Text="{Binding hourvalue}"></TextBlock>                   
            </StackPanel>
       </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

并将代码更改为:

    private void UpdateDataContext(object hrvalueinitially)
    {
        List<Roundedhour> hours = new List<Roundedhour>();
        hours.Add(new Roundedhour()
        {
            hourvalue = hrvalueinitially.ToString()
        });

        //Set the ItemsSource in code: => remove your ItemsSource Binding from XAML
        listBox.ItemsSource = hours;
    }
private void UpdateDataContext(对象hrvalueInitialized)
{
列表小时数=新列表();
小时数。添加(新的四舍五入小时数()
{
hourvalue=hrvalueinitially.ToString()
});
//在代码中设置ItemsSource:=>从XAML中删除ItemsSource绑定
listBox.ItemsSource=小时;
}
或者您可以使用“MVVM”方法:

    public class MyViewModel : INotifyPropertyChanged
    {
        //IMPLEMENT INotifyPropertyChanged HERE PLS

        public ObservableCollection<RoundedHour> Collection { get; set; } = new ObservableCollection<RoundedHour>();

        private void AddToCollection(object hrvalueinitially)
        {
            Collection.Add(new RoundedHour()
            {
                hourvalue = hrvalueinitially.ToString()
            });
            OnPropertyChanged("Collection");
        }

        //Make sure to set your Windows DataContext to an Instance of this Class
    }
公共类MyViewModel:INotifyPropertyChanged
{
//请在此处更改InotifyProperty
公共ObservableCollection集合{get;set;}=new ObservableCollection();
私有void AddToCollection(对象hrvalueinitially)
{
Collection.Add(新的RoundedHour()
{
hourvalue=hrvalueinitially.ToString()
});
不动产变更(“收款”);
}
//确保将Windows DataContext设置为此类的实例
}

您的
DataContext
需要有一个
public
属性,该属性包含
{get;}(getter)
以从
绑定中获取值。还要注意,
Roundedhour
中的属性称为
hourvalue
而不是
roundedhourvalue
。出现绑定问题时,请始终检查VisualStudio中的
输出
-窗口。-在这里,您将看到缺少的内容…我更改了文本={Binding hourvalue},但值未绑定到标签。您应该阅读有关System.Windows的信息。数据错误:40:BindingExpression路径错误:“对象”“字符串”(HashCode=-2067273858)上未找到obj属性。BindingExpression:Path=obj;DataItem='String'(HashCode=-2067273858);目标元素为“标签”(名称=“”);目标属性为'Content'(类型为'Object'),this.ItemsSource=obj;获取this.ItemsSource中的值,但该值未绑定到标签。如果使用“Listbox”,并且它具有ItemsSource属性,必须在codebehind上分配该属性,并且无法在代码示例中完成:this.DataContext=this;您需要为itemssource分配一个集合,如(List、Ienumerable等)。所以你还没有把任何东西和你的观点联系起来。
string roundedhourvalue = obj.hourvalue;
<ListBox ItemsSource="{Binding RoundedHours}" x:Name="ListBox">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" Margin="5" >
               <TextBlock Text="{Binding hourvalue}"></TextBlock>                   
            </StackPanel>
       </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
    private void UpdateDataContext(object hrvalueinitially)
    {
        List<Roundedhour> hours = new List<Roundedhour>();
        hours.Add(new Roundedhour()
        {
            hourvalue = hrvalueinitially.ToString()
        });

        //Set the ItemsSource in code: => remove your ItemsSource Binding from XAML
        listBox.ItemsSource = hours;
    }
    public class MyViewModel : INotifyPropertyChanged
    {
        //IMPLEMENT INotifyPropertyChanged HERE PLS

        public ObservableCollection<RoundedHour> Collection { get; set; } = new ObservableCollection<RoundedHour>();

        private void AddToCollection(object hrvalueinitially)
        {
            Collection.Add(new RoundedHour()
            {
                hourvalue = hrvalueinitially.ToString()
            });
            OnPropertyChanged("Collection");
        }

        //Make sure to set your Windows DataContext to an Instance of this Class
    }