如何在我的“CustomControl”标记中指定“Mode”和“UpdateSourceTrigger”?

时间:2012-06-20 07:10:24

标签: c# xaml data-binding custom-controls devexpress

拥有由CustomControlComboBox组成的Button

绑定工作正常,但Set只发生在LostFocus上。

这是我的CustomControl标记:

<Style TargetType="{x:Type cba:CustomComboBoxEditView}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type cba:CustomComboBoxEditView}">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>

                    <dxe:ComboBoxEdit x:Name="ComboBoxEdit"
                                      Height="Auto" 
                                      Width="Auto"
                                      Grid.Column="0"
                                      ImmediatePopup="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ImmediatePopup}"
                                      AutoComplete="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=AutoComplete}"
                                      IsEnabled="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsEnabled}"
                                      Visibility="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Visibility}"
                                      VerticalAlignment="Stretch" 
                                      HorizontalAlignment="Stretch"
                                      ItemsSource="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ItemsSource}"
                                      DisplayMember="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DisplayMember}"
                                      ValueMember="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ValueMember}"
                                      EditValue="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=EditValue}"/>

                    <dxe:ButtonEdit ShowText="False"
                                    Grid.Column="1"
                                    AllowDefaultButton="False"
                                    IsEnabled="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsEnabled}"
                                    Visibility="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Visibility}">
                        <dxe:ButtonInfo x:Name="PART_btnView" GlyphKind="Search" ToolTipService.ToolTip="View" />
                    </dxe:ButtonEdit>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

正常ComboBoxEdit。绑定看起来如下:

EditValue="{Binding Path=SomePath, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"

这是我使用CustomControl时的标记:

<cba:CustomComboBoxEditView Name="someName"
                     ItemsSource="{Binding Path=SomeSource, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
                     DisplayMember="DisplayMember"
                     ValueMember="ValueMember"
                     EditValue="{Binding Path=SomePath, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=False}"/>
  1. 如何指定&#34;模式&#34;和&#34; UpdateSourceTrigger&#34;在我的CustomControl's标记?
  2. CustomControl Set LostFocus只在CustomControl上发生了什么?
  3. 修改

    public class CustomComboBoxEditView : Control { #region Fields public static readonly DependencyProperty ImmediatePopupProperty; public static readonly DependencyProperty AutoCompleteProperty; public static readonly DependencyProperty ItemsSourceProperty; public static readonly DependencyProperty DisplayMemberProperty; public static readonly DependencyProperty ValueMemberProperty; public static readonly DependencyProperty EditValueProperty; public static readonly DependencyProperty SystemAppEntityViewCodeProperty; #endregion #region Constructor /// <summary> /// Default constructor. /// </summary> static CustomComboBoxEditView() { // Initialize as lookless control DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomComboBoxEditView), new FrameworkPropertyMetadata(typeof(CustomComboBoxEditView))); // Initialize dependency properties ImmediatePopupProperty = DependencyProperty.Register("ImmediatePopup", typeof(bool), typeof(CustomComboBoxEditView), new UIPropertyMetadata(null)); AutoCompleteProperty = DependencyProperty.Register("AutoComplete", typeof(bool), typeof(CustomComboBoxEditView), new UIPropertyMetadata(null)); ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(object), typeof(CustomComboBoxEditView), new UIPropertyMetadata(null)); DisplayMemberProperty = DependencyProperty.Register("DisplayMember", typeof(string), typeof(CustomComboBoxEditView), new UIPropertyMetadata(null)); ValueMemberProperty = DependencyProperty.Register("ValueMember", typeof(string), typeof(CustomComboBoxEditView), new UIPropertyMetadata(null)); EditValueProperty = DependencyProperty.Register("EditValue", typeof(object), typeof(CustomComboBoxEditView), new UIPropertyMetadata(null)); SystemAppEntityViewCodeProperty = DependencyProperty.Register("SystemAppEntityViewCode", typeof(string), typeof(CustomComboBoxEditView), new UIPropertyMetadata(null)); } #endregion #region Custom Control Properties public bool ImmediatePopup { get { return (bool)GetValue(ImmediatePopupProperty); } set { SetValue(ImmediatePopupProperty, value); } } public bool AutoComplete { get { return (bool)GetValue(AutoCompleteProperty); } set { SetValue(AutoCompleteProperty, value); } } public object ItemsSource { get { return (object)GetValue(ItemsSourceProperty); } set { SetValue(ItemsSourceProperty, value); } } public string DisplayMember { get { return (string)GetValue(DisplayMemberProperty); } set { SetValue(DisplayMemberProperty, value); } } public string ValueMember { get { return (string)GetValue(ValueMemberProperty); } set { SetValue(ValueMemberProperty, value); } } public object EditValue { get { return (object)GetValue(EditValueProperty); } set { SetValue(EditValueProperty, value); } } public string SystemAppEntityViewCode { get { return (string)GetValue(SystemAppEntityViewCodeProperty); } set { SetValue(SystemAppEntityViewCodeProperty, value); } } #endregion } 后面的代码:

    {{1}}

1 个答案:

答案 0 :(得分:0)

找到了解决方法:

注册PopupClosed Event,然后设置Property

public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        var btnComboBoxEdit = Template.FindName("ComboBoxEdit", this) as ComboBoxEdit;
        btnComboBoxEdit.PopupClosed +=new ClosePopupEventHandler(btnComboBoxEdit_PopupClosed);
    }

private void btnComboBoxEdit_PopupClosed(object sender, ClosePopupEventArgs e)
    {
        EditValue = e.EditValue;
    }