开发者

Displaying member in comboboxcolumn only after clicking column

开发者 https://www.devze.com 2023-04-02 02:12 出处:网络
I have a control, when I need dislay person with two column: -fullname -best friend The problem is , that property BestFriend on Person is an object.

I have a control, when I need dislay person with two column: -fullname -best friend

The problem is , that property BestFriend on Person is an object. At start Person has his own BestFriend, but he can change it from combobox column.

Now, after control loaded the column with bestfriend is blank. When I doubleclick at this column I can change bestfirend, and it sets bestfriend of this person.

But what I must to do to have at start not blank column?

I think, that the problem is, that control can't match bestfriend, with collection of bestfriend, so I think that I must match them by id, but I don't know how can I do ti.

<UserControl x:Class="MvvmLight1.MainPage"
             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"
             xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" mc:Ignorable="d"
             Height="300"
             Width="300"
             DataContext="{Binding Main, Source={StaticResource Locator}}">



            <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Skins/MainSkin.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>

    <Grid x:Name="LayoutRoot">

        <telerik:RadGridView x:Name="grdSrL"
                             AutoGenerateColumns="False"
                             SelectionMode="Single"
                             IsReadOnly="False"
                             IsFilteringAllowed="True"
                             Height="386"
                             Width="460"
                             HorizontalAlignment="Left"
                             CanUserDeleteRows="False"
                             CanUserInsertRows="True"
                             CanUserReorderColumns="False"
                             CanUserResizeColumns="True" 
                             ItemsSource="{Binding Persons}">
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn DataMemberBinding="{Binding FullName}" IsReadOnly="True" Header="FullName" />
                <telerik:GridViewComboBoxColumn ItemsSource="{Binding Friends,Source={StaticResource Main}}" ItemsSourceBinding="{Binding Friends,Source={StaticResource Main}}" Header="1st"
                                                DataMemberBinding="{Binding BestFriend}"        

      开发者_StackOverflow              DisplayMemberPath="FullName" />


            </telerik:RadGridView.Columns>
        </telerik:RadGridView>
    </Grid>
</UserControl>

the main model:

namespace MvvmLight1
{
    public class Person:INotifyPropertyChanged
    {
        private string _fullName;

        public string FullName
        {
            get { return _fullName; }
            set
            {
                if (_fullName!=value)
                {
                    _fullName = value;
                    OnPropertyChanged("FullName");
                }
            }
        }

        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }

        public Person BestFirend
        {
            get { return _bestFirend; }
            set
            {
                if (_bestFirend!=value)
                {
                    _bestFirend = value;
                    OnPropertyChanged("BestFirend");
                }
            }
        }

        private int _id;

        private Person _bestFirend;

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

and viewmodel:

using System.Collections.ObjectModel;
using GalaSoft.MvvmLight;

namespace MvvmLight1.ViewModel
{
    public class MainViewModel : ViewModelBase
    {

        public MainViewModel()
        {
            for (int i = 0; i < 3; i++)
            {
                var friend = new Person() {FullName = "Name" + (i + 3).ToString()};
                _friends.Add(friend);
                _persons.Add(new Person(){FullName = "Name"+i.ToString(),Id = i,BestFirend = friend});
            }
        }

        private ObservableCollection<Person> _persons=new ObservableCollection<Person>();


        public ObservableCollection<Person> Persons
        {
            get { return _persons; }
            set
            {
                _persons = value;
            }
        }

        public ObservableCollection<Person> Friends
        {
            get { return _friends; }
            set
            {
                _friends = value;
            }
        }

        private ObservableCollection<Person> _friends=new ObservableCollection<Person>();

    }
}

and app xaml

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             x:Class="MvvmLight1.App"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:vm="clr-namespace:MvvmLight1.ViewModel"
             mc:Ignorable="d">
    <Application.Resources>
        <!--Global View Model Locator-->
        <vm:ViewModelLocator x:Key="Locator"
                             d:IsDataSource="True" />

        <vm:MainViewModel x:Key="Main"/>
    </Application.Resources>
</Application>


Not an expert on GridViewComboBoxColumn, but could it be that it is looking an instance of an object in the bound list, and that instance is not in it?

With "normal" ComboBoxes you got the choice whether you use value binding or item binding. In case of itembindng, the ComboBox looks for the same instance in the list of values. If it cannot find it it does not select any item.

In case of Valuebinding, the SelectedValue is compared to the value specified by SelectedValuePath. This then means that there is no requirement that the list entry and the selected entry are the same instance.

But as I said, this is for box standard ComboBoxes, as for the Telerik controls ... I don't really know. But from my experience with them (with WebForm controls) they are a helpful bunch, if you ask questions in their user support forums.

0

精彩评论

暂无评论...
验证码 换一张
取 消