简体   繁体   English

Silverlight中是否有这样的内置控件?

[英]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). 我有一个带有ID和描述的项目列表(如果需要,我可以引入键值集合)。 What i need is control that binded to viewmodel id property, but shows description of corresponding item/pair on it. 我需要的是绑定到viewmodel id属性的控件,但是在其上显示了相应项/对的描述。 Closest example i know is combobox, where i set DisplayMemberPath and SelectedValue/SelectedValuePath, but i don't need dropdown. 我知道的最接近的示例是组合框,在其中设置了DisplayMemberPath和SelectedValue / SelectedValuePath,但是我不需要下拉菜单。 So is there any in-built control in Silverlight for this? 那么,Silverlight中是否有任何内置控件可用于此目的?

(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) (当然,我可以自己编写一个代码,这很容易,我什至可以为viewmodel放置一些逻辑以获取所需的对并将其描述绑定到简单的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. ItemClass包含两个属性:ID和Description。 I can place this control on the page, bind Items, and one-way bind SelectedID. 我可以将此控件放在页面上,绑定项目,并单向绑定SelectedID。

Edit 2: well i didn't make SelectedID DependencyProperty so binding won't work, but i will fix it right away 编辑2:好吧,我没有制作SelectedID DependencyProperty,所​​以绑定将不起作用,但是我会立即修复它

Edit 3: first snippet was sloppy and didn't work properly, so i fixed it. 编辑3:第一个片段是草率的并且不能正常工作,所以我修复了它。

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 . 将列表绑定到例如ItemsControl

Set ItemsSource to your list of items ItemsSource设置为您list of items

Then override ToString on your Item providing it's 'yours' really. 然后在您的Item上覆盖ToStringToString它确实是“您的”。 If not you can make your own wrapper. 如果没有,您可以自己包装。

Within ToString output whatever is presenting your item, eg description. 在ToString输出中,无论显示什么内容,例如描述。

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 只需将所有内容放入视图模型并绑定到它-TextBox,即

Text={Binding SelectedText}

eg 例如
...in your view model add SelectedText and SelectedID (and Items if needed) - properly do OnPropertyChanged . ...在您的视图模型中添加SelectedTextSelectedID (以及所需的Items )-正确地执行OnPropertyChanged

Set SelectedID from view model or if 'bound' from another control that may change it. 从视图模型设置SelectedID,或者从另一个可能更改它的控件“绑定”设置。
Within set for SelectedID set the SelectedText. 在为SelectedID设置的范围内,设置SelectedText。
No need for a control for things like that, it's all data binding really. 不需要像这样的控件,实际上就是所有数据绑定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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