wpf自定义控件本地属性绑定
wpf custom control local property binding
我有一个名为 RoomAndPersonsAccommodationItem 的自定义控件,里面有 属性 个房间:
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 的设置类似于 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>
在 class 上面的 ListView- RoomsListView 在哪里我有这个代码:
RoomsListView.ItemsSource = reservation.Rooms;
//reservation.Rooms is List<RoomData>
问题在这里:
<Controls:RoomAndPersonsAccommodationItem Room="{Binding Path=., Mode=TwoWay}" HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" />
我想将 Room 属性 绑定到 ListView 上 ItemSource 的房间对象,但我收到 null。
有谁知道我哪里错了?也许在这里:Room="{Binding Path=., Mode=TwoWay}" 或 Dependency属性 声明?
谢谢
马丁
更新:
仍然无效...结果始终为空。我尝试了很多类似的选项来绑定 Tag 或 DataContext 但没有任何反应。我想问题不在 Dependency属性 中。它在某处绑定我的东西......
这又是代码:
private ReservationData reservation = null;
//Methods
private void fillForms()
{
//Logic
RoomsListView.ItemsSource = reservation.Rooms;
//Logic
}
这是带有 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>
RoomAndPersonsAccommodationItem 中的依赖项属性:
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); }
}
更新 2
已解决
上面的来源是正确的。错误的结果来自于我在构造函数完成之前检查了房间 属性。当我检查构造函数后的结果时,一切正常!谢谢!
试试这个,一个 属性 和一个依赖项 属性 属性:
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); }
}
您也可以从绑定中删除 "Path",这不是必需的。
我有一个名为 RoomAndPersonsAccommodationItem 的自定义控件,里面有 属性 个房间:
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 的设置类似于 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>
在 class 上面的 ListView- RoomsListView 在哪里我有这个代码:
RoomsListView.ItemsSource = reservation.Rooms;
//reservation.Rooms is List<RoomData>
问题在这里:
<Controls:RoomAndPersonsAccommodationItem Room="{Binding Path=., Mode=TwoWay}" HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" />
我想将 Room 属性 绑定到 ListView 上 ItemSource 的房间对象,但我收到 null。 有谁知道我哪里错了?也许在这里:Room="{Binding Path=., Mode=TwoWay}" 或 Dependency属性 声明?
谢谢 马丁
更新:
仍然无效...结果始终为空。我尝试了很多类似的选项来绑定 Tag 或 DataContext 但没有任何反应。我想问题不在 Dependency属性 中。它在某处绑定我的东西...... 这又是代码:
private ReservationData reservation = null;
//Methods
private void fillForms()
{
//Logic
RoomsListView.ItemsSource = reservation.Rooms;
//Logic
}
这是带有 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>
RoomAndPersonsAccommodationItem 中的依赖项属性:
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); }
}
更新 2
已解决 上面的来源是正确的。错误的结果来自于我在构造函数完成之前检查了房间 属性。当我检查构造函数后的结果时,一切正常!谢谢!
试试这个,一个 属性 和一个依赖项 属性 属性:
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); }
}
您也可以从绑定中删除 "Path",这不是必需的。