繁体   English   中英

将数据绑定到自定义UserControl

[英]Binding data to custom UserControl

我想将ObservableCollection绑定到我的自定义UserControl上,但是当我想添加新元素并且快要完成的时候,但是当我想添加新元素来收集我的应用程序崩溃时,没有理由。 我以为可能是变量不一致。 但是我随nothig一起尝试了对象/字符串/字符串。 也许我一开始做错了什么?

自定义UserControl XAML

<UserControl x:Class="backtrackPrototype.checklistItem"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    d:DesignHeight="80" d:DesignWidth="480">

    <Grid x:Name="LayoutRoot">
        <TextBlock 
            x:Name="label" 
            MaxHeight="407"  
            HorizontalAlignment="Left" 
            Margin="0,17,0,16" 
            VerticalAlignment="Center"
            Text="{Binding Path=Title, ElementName=checklistItem}"/>
        <CheckBox x:Name="checkbox" HorizontalAlignment="Right" VerticalAlignment="Center" Checked="checkbox_Checked" Unchecked="checkbox_Unchecked" />
    </Grid>
</UserControl>

自定义UserControl

public partial class checklistItem : UserControl 
{ 

      public string Title
      {
          get { return (string)this.GetValue(TitleProperty); }
          set { this.SetValue(TitleProperty, value); } 
      }

      // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
      public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(checklistItem), new PropertyMetadata(0));  

      public checklistItem() 
      {
            InitializeComponent();
      }

      public bool isChecked() 
      {
          if ((bool)checkbox.IsChecked) return true;
          return false;
      }

      private void checkbox_Checked(object sender, RoutedEventArgs e) 
      {
          //title.Foreground = Application.Current.Resources["PhoneAccentColor"];
          label.Foreground = (SolidColorBrush)Application.Current.Resources["PhoneAccentBrush"];
      }

      private void checkbox_Unchecked(object sender, RoutedEventArgs e) 
      {
          label.Foreground = new SolidColorBrush(Colors.White);
      }
}

主页中的XAML

<ListBox Height="479" ItemsSource="{Binding}" x:Name="CheckList">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <local:checklistItem Title="{Binding Title}" Width="397"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox

主页中的CS公共ObservableCollection ListItems = new ObservableCollection();

ListItem item = new ListItem("Nowe",false);
ListItems.Add(item);
CheckList.DataContext = ListItems;

最后列出

public class ListItem 
{
    public string Title { get; set; }
    public bool Checked { get; set; }

    public ListItem(string title, bool isChecked=false) 
    {
        Title = title;
        Checked = isChecked;
    }  
}

我需要一个能以崭新的眼光看代码的人。 谢谢。


更新:

在我的CustomControl中访问控件还有什么呢?我在该问题中使用了第一个anwser。 另外,我还更新了UserControl XAML。


更新V2

当我使用由local:checklistItem插入的<TextBox Title="{Binding Title}" Width="397"/> ,它可以正常工作。


更新V3

OK证明我没有向UserControl中添加适当的DataContext,所以现在checklistItem构造函数看起来像这样

public checklistItem() {
    InitializeComponent();
    this.DataContext = this;
}

并且控件可以正确添加,但是现在无法为Title绑定。 因为当我对某些标题进行硬编码时,它起作用了,但绑定不起作用。 但是相同的绑定适用于默认文本框。

我认为您需要设置itemsSource而不是DataContext。 在列表框上设置DataContext不会生成模板项

取出

ItemsSource="{Binding}"

从Xaml更改

CheckList.DataContext = ListItems;

CheckList.ItemsSource = ListItems;

我认为您在XAML中的绑定可能不正确。 绑定中的元素名称是LayoutRoot,这是网格的名称,而不是UserControl。

它可能正在尝试在网格上查找一个名为Title的属性,而该属性却没有。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM