简体   繁体   中英

wpf custom control local property binding

I have custom control named- RoomAndPersonsAccommodationItem and inside I have 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:

<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:

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. Does anyone know where I'm wrong? Maybe here: Room="{Binding Path=., Mode=TwoWay}" or on DependencyProperty declaration?

Thanks Martin

UPDATE:

Still doesn't work... The result always is null. I tried a lot of options like that to bind the Tag or DataContext but nothing happens. I suppose that the problem is not in the 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:

<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:

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

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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