自定义WPF库 [英] Custom WPF Library

查看:104
本文介绍了自定义WPF库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我目前正在尝试根据已经存在的第三方库(项目要求)创建一个自定义WPF图形库.该库必须:

-只需包含3rd party库的选定组件(为了简化本文的编写,我们只需要3rdPartyComboBox和3rdPartyTextBox)
-只需暴露3rdPartyComboBox和3rdPartyTextBox中的某些属性.
-如果可能的话,通过库使用标准的ComboBox和TextBox.
-我们的CustomLibraryComboBox(和CLTextBox)将显示第三方属性,并实现我们的自定义属性.可以说,我们需要第3方库中的"N"个属性和"M"个自定义属性:

Hi,

Im currently trying to create a custom WPF Graphic Library, based on an already existing 3rd party library (project requirements). This library must:

- Just include the selected components of the 3rd party library (in order to ease the writing of this post, lets say we just need the 3rdPartyComboBox and the 3rdPartyTextBox)
- Just expose certain attributes from 3rdPartyComboBox and 3rdPartyTextBox.
- Use standard ComboBox and TextBox, if possible through the library.
- Our CustomLibraryComboBox (and CLTextBox) will expose the 3rd party attributes, as well as implement our custom attributes. Lets say we need "N" attributes from the 3rd party library and "M" custom attributes:

<my:CustomComboBox
  3rdPartyAttrib1
  3rdPartyAttrib2
  .
  3rdPartyAttribN
  CustomAttrib1
  CustomAttrib2
  .
  CustomAttribM
/>




我已经有一个测试实现,它基本上为每个必需的Control创建一个 CustomControl .它创建所需的控件作为唯一的可视子控件,并将所选属性公开为Dependency属性:




I already have a testing implementation, which basically creates a CustomControl for each required Control. It creates the desired Control as its only visual child, and exposed the selected attributes as Dependency properties:

public class CustomControl1 : Control
private 3rdPartyTextBox _combobox = null;
static CustomControl1()
{
    DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1),
        new FrameworkPropertyMetadata(typeof(CustomControl1)));

}
public CustomControl1() : base()
{
   _combobox = new 3rdPartyTextBox();
   this.AddVisualChild(_combobox);
}
public int SelectedIndex
{
   get { return (int)GetValue(SelectedIndexProperty); }
   set { SetValue(SelectedIndexProperty, value); }
}
public static readonly DependencyProperty SelectedIndexProperty =
   DependencyProperty.Register(
       "SelectedIndex", typeof(int),
       typeof(CustomControl1), new FrameworkPropertyMetadata(0,
                                  new PropertyChangedCallback(ChangeSelectedIndex))
       );
private static void ChangeSelectedIndex(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
   (source as CustomControl1).UpdateSelectedIndex((int)e.NewValue);
}
private void UpdateSelectedIndex(int sel)
{
   _combobox.SelectedIndex = sel;
}




这种工作,但控件的行为不是预期的.似乎2way绑定不起作用.我尝试重新映射相应的RoutedEvents(SelectionChanged等),但没有成功.

我还尝试将整个3rdPartyControl公开为属性:




This kind of works, but the behavior of the Control is not the expected. It seems that 2way binding doesn''t work. I tryied remaping the corresponding RoutedEvents (SelectionChanged, etc) with no success.

I also tryied exposing the whole 3rdPartyControl as a Property:

public 3rdPartyTextBox Child
{
   get
   {
       return _combobox;
   }
   set
   {
       if (_combobox != value)
       {
           this.RemoveVisualChild(_combobox);
           _combobox = value;
           this.AddVisualChild(_combobox);
       }
   }
}




此方法有效,但是 XAML 要求使用它不是我们想要的:




This works, but the XAML required to use it is not what we want:

<my:CustomControl1>
    <my:CustomControl1.Child>
        <3rdPartyLibrary:3rdPartyComboBox

            ItemsSource="{Binding DynamicCollection}"

            SelectedIndex="{Binding SelectedIndex, Mode=TwoWay}" ItemTemplate="{StaticResource ComboBoxItemTemplate}">
        </3rdPartyLibrary:3rdPartyComboBox>
    </my:CustomControl1.Child>
</my:CustomControl1>




关于如何实现这样的事情有什么想法吗?
谢谢,

-
罗伯特.




Any ideas on how can we implement such a thing?
Thanks,

--
Robert.

推荐答案

对于所有感兴趣的人,可以在这里更好地解释该问题: ^ ]

在那里,您还将找到有关此问题的解决方法.
For all those interested, the problem is better explained here: http://stackoverflow.com/questions/2820378/wrapped-wpf-control[^]

There you will also find a workaround on this issue.


这篇关于自定义WPF库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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