[英]wpf custom control local property binding
I have custom control named- RoomAndPersonsAccommodationItem and inside I have Property Room: 我有一个名为-RoomAndPersonsAccommodationItem的自定义控件,而我里面有Property Room:
public static readonly DependencyProperty roomProperty =
DependencyProperty.Register("Room", typeof(RoomData),
typeof(RoomAndPersonsAccommodationItem), new FrameworkPropertyMetadata(null));
public RoomData RoomProperty
{
get { return GetValue(roomProperty) as RoomData; }
set { SetValue(roomProperty, value); }
}
private RoomData room = null;
public RoomData Room
{
get { return room; }
set { room = value; }
}
RoomAndPersonsAccommodationItem is set like DataTemplate on ListView: RoomAndPersonsAccommodationItem的设置类似于ListView上的DataTemplate:
<ListView Name="RoomsListView" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"></StackPanel>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<Controls:RoomAndPersonsAccommodationItem Room="{Binding Path=., Mode=TwoWay}" HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
In class where is the above ListView- RoomsListView I have this code: 在上面的ListView- RoomsListView的类中,我有以下代码:
RoomsListView.ItemsSource = reservation.Rooms;
//reservation.Rooms is List<RoomData>
The problem is here: 问题在这里:
<Controls:RoomAndPersonsAccommodationItem Room="{Binding Path=., Mode=TwoWay}" HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" />
I want to bind Room property to room object from ItemSource on the ListView but I receive null. 我想将Room属性从ListView上的ItemSource绑定到room对象,但是却收到null。 Does anyone know where I'm wrong? 有人知道我错了吗? Maybe here: Room="{Binding Path=., Mode=TwoWay}" or on DependencyProperty declaration? 也许在这里:Room =“ {Binding Path =。,Mode = TwoWay}”或DependencyProperty声明?
Thanks Martin 谢谢马丁
UPDATE: 更新:
Still doesn't work... The result always is null. 仍然不起作用...结果始终为null。 I tried a lot of options like that to bind the Tag or DataContext but nothing happens. 我尝试了很多类似的方法来绑定Tag或DataContext,但没有任何反应。 I suppose that the problem is not in the DependencyProperty. 我想问题不在于DependencyProperty。 It is somewhere in binding I thing... This is the code again: 它在绑定我的东西的某处...这又是代码:
private ReservationData reservation = null;
//Methods
private void fillForms()
{
//Logic
RoomsListView.ItemsSource = reservation.Rooms;
//Logic
}
This is the ListView with DataTemplate: 这是带有DataTemplate的ListView:
<ListView Name="RoomsListView" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"></StackPanel>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<Controls:RoomAndPersonsAccommodationItem
Room="{Binding}"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
And DependencyProperty in RoomAndPersonsAccommodationItem: RoomAndPersonsAccommodationItem中的DependencyProperty:
public static readonly DependencyProperty RoomProperty =
DependencyProperty.Register(
"Room", typeof(RoomData), typeof(RoomAndPersonsAccommodationItem),
new FrameworkPropertyMetadata(null));
public RoomData Room
{
get { return (RoomData)GetValue(RoomProperty); }
set {
SetValue(RoomProperty, value); }
}
UPDATE 2 更新2
It's SOLVED The above source is correct. 已解决上面的来源是正确的。 The wrong result came from that I have checked the Room property before constructor has been finished. 错误的结果是我在构造函数完成之前检查了Room属性。 When I checked the result after constructor all was OK! 当我在构造函数之后检查结果时一切正常! Thank you! 谢谢!
Try this, one property and one dependency property for this property: 尝试一下,为此属性提供一个属性和一个依赖项属性:
public static readonly DependencyProperty RoomProperty =
DependencyProperty.Register(
"Room", typeof(RoomData), typeof(RoomAndPersonsAccommodationItem),
new FrameworkPropertyMetadata(null));
public RoomData Room
{
get { return (RoomData)GetValue(RoomProperty); }
set { SetValue(RoomProperty, value); }
}
You can also remove the "Path" from the binding, it is not necessary. 您也可以从绑定中删除“路径”,这不是必需的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.