简体   繁体   中英

Is there such built-in control in silverlight?

I have a list of items with id and description(i can introduce key-value collection instead if needed). What i need is control that binded to viewmodel id property, but shows description of corresponding item/pair on it. Closest example i know is combobox, where i set DisplayMemberPath and SelectedValue/SelectedValuePath, but i don't need dropdown. So is there any in-built control in Silverlight for this?

(of course i can code one myself, it's easy and I can even just put some logic for viewmodel to get pair i need and bind it's description to simple textblock)

Edit: To illustrate what funcionality i need i coded simple example class. It actually satisfies my needs, but i still want to know if i can use built-in control .

public class CollectionItemDisplayControl:TextBox
{
    public CollectionItemDisplayControl()
    {
        IsReadOnly = true;

    }

    public string SelectedID
    {
        get { return (string)GetValue(SelectedIDProperty); }
        set { SetValue(SelectedIDProperty, value); }
    }

    // Using a DependencyProperty as the backing store for SelectedID.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty SelectedIDProperty =
        DependencyProperty.Register("SelectedID", typeof(string), typeof(CollectionItemDisplayControl), new PropertyMetadata(new PropertyChangedCallback(OnSelectedIDChangedStatic)));


    private static void OnSelectedIDChangedStatic(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        CollectionItemDisplayControl originator = d as CollectionItemDisplayControl;
        if (originator != null)
        {
            originator.OnSelectedIDChanged(e);
        }
    }

    private void OnSelectedIDChanged(DependencyPropertyChangedEventArgs e)
    {
        string description = String.Empty;
        string value = e.NewValue as string;
        if (value != null)
        {
            foreach (var item in _items)
            {
                if (item.UniqueID == value)
                {
                    description = item.Description;
                    break;
                }
            }
        }
        Text = description;
    }        

    private IDataCollection _viewModel;
    public IDataCollection ViewModel
    {
        get { return _viewModel; }
        set
        {
            _viewModel = value;
            if (_viewModel != null)
            {
                _items = _viewModel.Items;
            }
        }
    }

    private ObservableCollection<IUnique> _items = new ObservableCollection<IUnique>();

}

ItemClass contains two properties: ID and Description. I can place this control on the page, bind Items, and one-way bind SelectedID.

Edit 2: well i didn't make SelectedID DependencyProperty so binding won't work, but i will fix it right away

Edit 3: first snippet was sloppy and didn't work properly, so i fixed it.

If I understood properly,

You just need the right binding implemented.

(you do need a list? not just a single item, even if single it's similar just any control)

Bind the list to eg ItemsControl .

Set ItemsSource to your list of items

Then override ToString on your Item providing it's 'yours' really. If not you can make your own wrapper.

Within ToString output whatever is presenting your item, eg description.

That's a quickest solution, you can also make item template as you want.

EDIT:
well just put everything in the view model and bind to it - the TextBox, ie

Text={Binding SelectedText}

eg
...in your view model add SelectedText and SelectedID (and Items if needed) - properly do OnPropertyChanged .

Set SelectedID from view model or if 'bound' from another control that may change it.
Within set for SelectedID set the SelectedText.
No need for a control for things like that, it's all data binding really.

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