我如何可以设置一个用户控件绑定到一个COMBOX? [英] How can I set a binding to a Combox in a UserControl?

查看:82
本文介绍了我如何可以设置一个用户控件绑定到一个COMBOX?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了好几天在这个问题上似乎无法得到它的工作。

I have spent several days on this issue and can't seem to get it to work.

我也没有保存到XAML文件具有以下code用户控件:

I have a user control that is saved out to a xaml file with the following code:

        StringBuilder outstr = new StringBuilder();
        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;
        settings.OmitXmlDeclaration = true;

        XamlDesignerSerializationManager dsm = new 
        XamlDesignerSerializationManager(XmlWriter.Create(outstr, settings));
        dsm.XamlWriterMode = XamlWriterMode.Expression;

        System.Windows.Markup.XamlWriter.Save(test1, dsm);

        String saveCard = outstr.ToString();

        File.WriteAllText("inputEnum.xaml", saveCard);

的XAML用户控制:

Xaml for the user control:

<Grid >
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="{Binding DescriptionWidth}" />
        <ColumnDefinition Width="{Binding ValueWidth}" />
    </Grid.ColumnDefinitions>

    <ComboBox Grid.Column="1" Background="White" FontSize="{Binding FontSizeValue}" Width="Auto" 
              Padding="10,0,5,0" ItemsSource="{Binding ComboItemsProperty}" SelectedIndex="{Binding EnumSelectedIndex}">

    </ComboBox>
</Grid>

ComboBox中的ItemsSource是什么,是给我的问题。
当我出去拯救这个用户控件的一个实例到文件,从我的理解,{结合ComboItemsProperty}丢失。所以,在我的用户的构造函数中我有:

The ItemsSource in the combobox is what is giving me problems. When I save an instance of this usercontrol out to a file, from my understanding, the {Binding ComboItemsProperty} is lost. So, in the constructor of my usercontrol I have:

public UserInputEnum()
{
     InitializeComponent();

     Binding bind = new Binding();
     bind.Mode = BindingMode.TwoWay;
     bind.Source = this;
     bind.Path = new PropertyPath("ComboItemsProperty");
     this.SetBinding(ComboBox.ItemsSourceProperty, bind);
}

下面是我的财产,并将更改的方法:

Here is my property and the changed method:

    EnumItemsCollection ComboItems = new EnumItemsCollection();
    public EnumItemsCollection ComboItemsProperty
    {
        get { return ComboItems; }
        set 
        { 
            ComboItems = value;
            OnPropertyChanged("ComboItemsProperty"); 
        }
    }
    public void OnPropertyChanged(string propertyName)
    {
        getEnumItems(this.ComboItemsProperty, this.EnumSelectedIndex, this.ID, this.SubmodeID);
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this.ComboItems, new PropertyChangedEventArgs(propertyName));
        }
    }

刚一说明。 EnumItemsCollection是一个简单的类继承了的ObservableCollection。没有别的此类。 (不知道这是否有差别)。

Just a note. EnumItemsCollection is a simple class that inherits off ObservableCollection. There is nothing else to this class. (not sure if this makes a difference).

我觉得这应该工作,但是当我通过XAMLReader加载XAML文件,我的组合框项目将不会更新。

I think this should work but when when I load the XAML file through the XAMLReader, my combobox items won't update.

编辑:

我跑不是从XAML加载,而是在MainWindow.xaml。

I ran a little test on an instance of user control that wasn't loaded from XAML but is in the MainWindow.xaml.

一切工作正常。当我加入到ComboItemsProperty,组合框的更新。

Everything works fine. When I add to the ComboItemsProperty, the combobox updates.

所以,我拿走了{结合ComboItemsProperty},并试图以设置code结合如上变化的这到用户控件的实例。没有工作。这告诉我,这是不正常的约束力code。

So, I took away the {Binding ComboItemsProperty} and tried to set the binding in the code as above changing 'this' to the instance of the user control. Didn't work. This tells me it is the binding code that is not functioning correctly.

我相当肯定是bind.Source行是问题。当它在用户控件我不确定什么放在那里。

I'm fairly certain is the bind.Source line that is the issue. When it is in a UserControl I am unsure of what to put there.

编辑:

code,它从文件装载用户控件:

Code that loads usercontrol from file:

FileStream stream = File.Open("usercontrol.xaml", FileMode.Open, FileAccess.Read);
ComboBox cmb = System.Windows.Markup.XamlReader.Load(stream) as ComboBox;

它加载完美的罚款。绑定只是不工作(的ItemsSource = {结合ComboItemsProperty}),因为绑定不会被保存了。

It loads perfectly fine. The Binding just isn't working(ItemsSource={Binding ComboItemsProperty}) because Bindings aren't saved out.

我从文件中加载它,因为这一计划将在某种意义上许多用户界面。每个人都会使用该程序的不同的人被载入。

I load it from a file because this program will have many User Interfaces in a sense. Each one will be loaded by a different person using the program.

推荐答案

您需要包含你的财产的 ComboItemsProperty 实例的设定范围内。因此,而不是'这个'U应将其设置为 this.DataContext 或含已定义的属性的ItemSource其他类的对象实例。

You need to the set context of the instance containing your property ComboItemsProperty. So instead of 'this' u should set it to this.DataContext or other class object instance containing the ItemSource property you have defined..

试试这个,

Binding bind = new Binding();  
bind.Mode = BindingMode.TwoWay;  
bind.Source = this.DataContext;  
bind.Path = new PropertyPath("ComboItemsProperty");  
this.SetBinding(ComboBox.ItemsSourceProperty, bind);  

更新

根据 XamlWriter.Save系列化限制在MSDN上提供,


  • 许多原来XAML文件的设计时属性可能已经被优化或该XAML加载在内存中的对象时遗失的,而不是当你调用保存序列化preserved。

  • 要通过各种标记扩展格式的人造物体,如静态资源绑定,将被取消引用通用参考序列化过程。

  • Many design-time properties of the original XAML file may already be optimized or lost by the time that the XAML is loaded as in-memory objects, and are not preserved when you call Save to serialize.
  • Common references to objects made by various markup extension formats, such as StaticResource or Binding, will be dereferenced by the serialization process.

结论,我现在做出来的是你不能直接通过连载加载用户控件作为一个整体 - 的反序列化程序 XAML 。我想你可以通过序列化加载对象实例 - 反序列化程序上的的DataContext 的用户控件即自定义列表中( S)或对象(S)你有数据绑定。

Conclusion, that I made out now is you cannot directly load the UserControl as whole by Serialization - Deserialization procedure of XAML. I think you can load the object instances by Serialization - Deserialization procedure on the DataContext of the UserControl i.e. the custom list(s) or object(s) you have databound.

这篇关于我如何可以设置一个用户控件绑定到一个COMBOX?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆