- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
<分区>
在我的项目中,我需要将数据绑定(bind)到驻留在另一个 UserControl 中的 UserControl。为了简洁起见,我创建了一个概念相似但非常简单的项目。
假设我正在创建一个电话簿应用程序,其中有两个用户控件,如下所示。
窗口中的大蓝色框将是一个 UserControl,它显示所有者的姓名 (Jane Doe),其中的每个黄色框也是 UserControl,它显示联系人的姓名和电话号码。
我有两个类,我持有相关数据,如下:
public class Person
{
public string Name { get; set; }
public string Phone { get; set; }
public Person() { }
}
public class PhoneBook
{
public string OwnerName { get; set; }
public ObservableCollection<Person> ContactList { get; set; }
public PhoneBook() { }
}
在我的 MainWindow
中,我使用 ViewModel 并像这样绑定(bind)到 PhoneBook
UserControl:
<Window x:Class="UserControlDataBinding.MainWindow"
Title="MainWindow" Height="300" Width="350"
DataContext="{Binding Source={StaticResource mainViewModelLocator},Path=ViewModelPhoneBook}">
<Grid>
<local:UCPhoneBook x:Name="ucPhoneBook" MainPhoneBook="{Binding PhoneBookData}"></local:UCPhoneBook>
</Grid>
</Window>
PhoneBookData
是 ViewModel 上的 PhoneBook
类的实例。
我的两个用户控件及其 DependancyProperties
如下所示。
UCPhoneBook 用户控件(蓝色框):
在这里,我使用 ItemsControl
动态绑定(bind) UCPerson
UserControl,这样我就可以在运行时添加任意数量的控件。
<UserControl x:Class="UserControlDataBinding.UCPhoneBook"
d:DesignHeight="300" d:DesignWidth="450">
<Canvas x:Name="ucCanvasPhoneBook" Background="White">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<GroupBox Grid.Row="0" Header="Phonebook">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<Label Grid.Row="0" Name="lblOwnerName"
Content="{Binding Path=MainPhoneBook.OwnerName}">
</Label>
</Grid>
</GroupBox>
<ItemsControl Grid.Row="1"
ItemsSource="{Binding PhoneBookData.ContactList}">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type local:Person}">
<local:UCPerson PersonData="{Binding Person}"></local:UCPerson>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Grid>
</Canvas>
</UserControl>
它的DependancyProperty
:
public partial class UCPhoneBook : UserControl
{
private static readonly DependencyProperty PhoneBookProperty = DependencyProperty.Register("MainPhoneBook", typeof(PhoneBook), typeof(UCPhoneBook), new PropertyMetadata(null));
public PhoneBook MainPhoneBook
{
get { return (PhoneBook)GetValue(PhoneBookProperty); }
set { SetValue(PhoneBookProperty, value); }
}
public UCPhoneBook()
{
InitializeComponent();
ucCanvasPhoneBook.DataContext = this;
}
}
UCPerson 用户控件(黄色框):
<UserControl x:Class="UserControlDataBinding.UCPerson"
d:DesignHeight="26" d:DesignWidth="400">
<Canvas x:Name="ucCanvasPerson" Background="WhiteSmoke">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Name="lblName"
HorizontalAlignment="Left" VerticalAlignment="Center"
Content="{Binding Name}"></Label>
<Label Grid.Column="2" Name="lblPhone"
HorizontalAlignment="Right" VerticalAlignment="Center"
Content="{Binding Phone}"></Label>
</Grid>
</Canvas>
</UserControl>
它的DependancyProperty
:
public partial class UCPerson : UserControl
{
private static readonly DependencyProperty PersonProperty = DependencyProperty.Register("PersonData", typeof(Person), typeof(UCPerson), new PropertyMetadata(null));
public Person PersonData
{
get { return (Person)GetValue(PersonProperty); }
set { SetValue(PersonProperty, value); }
}
public UCPerson()
{
InitializeComponent();
ucCanvasPerson.DataContext = this;
}
}
当我运行它时,我可以在第一个用户控件(蓝色框)的顶部看到所有者的姓名,这很好。但是,它似乎没有正确绑定(bind) UCPerson
用户控件,我得到一个空列表,如下所示:
我的猜测是我没有正确绑定(bind)到第一个 UserControl 中的 ItemsControl
。我是 DataBinding 的新手,似乎无法弄清楚正确的方法是什么。
我在这里做错了什么?
我正在尝试在 android 中学习 ViewModel,在我的第一阶段学习中,我正在尝试使用 ViewModel 和 DataBinding 来更新 UI(TextView)。在 ViewModel
在 ASP.NET 中,您可以单独绑定(bind)控件(即 GridView1.DataBind() ),也可以调用 Page.DataBind()绑定(bind)页面上的所有控件。 这两个调用之间有
如何使用新的 dataBinder 插件进行双向绑定(bind)(从 UI 到数据以及从数据到 UI)? 非常感谢! 最佳答案 我们还没有这个,也许在 v1 之后。不确定您的用例是什么,但我们已经为每
我正在尝试将 Kotlin 与 DataBinding 一起使用,但我遇到了这个错误: Error:[kapt] An exception occurred: java.lang.annotation
我只是将我的项目转换为 androidX 并成功构建项目。但是当我想运行那个时间时,它在启用了 java 的 android studio 3.5.3 中创建了这个错误。 这是我的项目 gradle:
这个问题在这里已经有了答案: no cached version available for offline mode (11 个回答) 2年前关闭。 无法确定任务的依赖关系 :app:compile
这是一个与今天无关的老问题。请参阅 Android 开发人员的说明。与四年前相比,现在将数据绑定(bind)应用于您的项目要容易得多。 我正在阅读 https://developer.android.
异常(exception)情况如下: java.lang.NullPointerException: Attempt to read from field 'java.lang.Runnable an
构建项目时收到以下警告 DSL element 'android.dataBinding.enabled' is obsolete and has been replaced with 'androi
今天升级Android Studio 2.3后,构建项目失败。 当构建项目 gradle console 显示这个错误: FAILURE: Build failed with an exception
我有一个消息列表,我将这些消息的模板绑定(bind)到单击事件中,然后在该模板内,我有一个超链接绑定(bind)到 js 函数,该函数应该打开一个 mailto 页面。 但是,尽管 js 函数执行,但
我在 Wildfly 8.2.1 和 Glassfish 4.1 中使用 Spring Data JPA 部署 Spring MVC 应用程序时遇到问题(它在 Wildfly 10 中工作,但我不允许
我为横向创建了一个替代布局 XML 文件。使用 DataBindingUtil 的填充在纵向模式下工作正常,但在加载自定义 XML 文件时在横向模式下崩溃。 使用数据绑定(bind)的布局膨胀: cl
数据绑定(bind)到控件的可见属性时是否存在任何已知问题? 无论我的属性是什么,该控件始终不可见。 Public ReadOnly Property IsRibbonCategory() As Bo
使用数据绑定(bind),如何绑定(bind)使用值类型的新对象? 简单的例子: public class Person() { private string _firstName;
我有一个 Image 控件,它的源绑定(bind)到对象上的属性(字符串 url 到图像)。进行服务调用后,我使用新 URL 更新数据对象。在调用 PropertyChanged 事件后,它离开我的代
我想在传入的绑定(bind)整数上添加一个常量值。事实上,我有几个地方想要绑定(bind)到相同的源值但添加不同的常量。所以理想的解决方案是这样的...... (注意:这是一个展示这个想法的例子
我正在开发一个 Windows Phone 应用程序。我将 List 绑定(bind)到内容控制元素。 在转换器中,我返回指定参数的字符串。
天哪!我讨厌这个。为什么这么复杂? 我正在尝试做的事情: 我有一个包含多个用户控件的表单,每个控件都有一个数据网格。每个网格通过 .ItemSource 属性与 ObservableCollectio
我正在使用数据绑定(bind)并且我已经声明了 lateinit var对于绑定(bind)以及当我要去不同的 fragment Leaky canary 显示泄漏时。 fragment class
我是一名优秀的程序员,十分优秀!