2

ユーザー入力 (ハンドラー クラスの値) が (ItemsSource の) リストで見つかるかどうかを確認するために、検証クラスに ItemsSource オブジェクトを渡したいと思います。

ComboBox の ItemsSource をハンドラ クラス RestrictedComboBoxItemValidationRule に送信するにはどうすればよいですか? (またはItemsSourceの代わりに私のComboBox-controll)

<ComboBox Name="bms_ComboBox
          ItemsSource='{Binding Path="[BMS,ANR]"}'
          SelectedValuePath="F1"
          DisplayMemberPath="F1"
          IsEditable="True">
    <ComboBox.Text>
        <Binding Path="[BMS]">
            <Binding.ValidationRules>
                <t:RestrictedComboBoxItemValidationRule Sender={how I can submit ItemsSource of this ComboBox to handler class???}/>
            </Binding.ValidationRules>
        </Binding>
    </ComboBox.Text>
</ComboBox>

// ...

public class RestrictedComboBoxItemValidationRule : ValidationRule 
{
    public object Sender
    {
        get { return sender; }
        set { sender = value; }
    }

    public override ValidationResult Validate(object value, CultureInfo cultureInfo) 
    {
        ValidationResult vr = ValidationResult.ValidResult;

        if (comboText_inItemsSource == false) {
            vr = new ValidationResult(false, "The entered value is not included in list!");
        }
        return vr;
    }
4

2 に答える 2

0

ComboBox の ItemsSource を Sender プロパティにバインドする必要があります。ValidationRule は DependencyObject ではないためSender、単に DependencyProperty を作成することはできません。しかし、Josh Smith による優れた回避策があります: http://www.codeproject.com/Articles/18678/Attaching-a-Virtual-Branch-to-the-Logical-Tree-in

于 2012-11-23T09:47:03.867 に答える
-1

私は、少し複雑な方法を見つけました。しかし、それは機能します!これが方法です:

AttachedProperty「Restricted」を作成しました

RestrictedProperty = DependencyProperty.RegisterAttached
("Restricted", typeof(bool), typeof(Field), 
new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnChangedRestricted)));

OnChangedRestrictedで、ComboBox.Loadedのイベントハンドラーを追加します。

private static void OnChangedRestricted(DependencyObject o, DependencyPropertyChangedEventArgs e) {
        Control c = o as Control;
        if (c != null && (bool)e.NewValue == true) {
            if (c.GetType() == typeof(ComboBox)) {
                ((ComboBox)c).Loaded += new RoutedEventHandler(RestrictedComboBox_Loaded);

その理由は、vlideBindingExpressionが必要だからです。

static void RestrictedComboBox_Loaded
    (object sender, RoutedEventArgs e) {
    ComboBox cb = (ComboBox)sender;
    BindingExpression be = cb.GetBindingExpression
        (ComboBox.TextProperty);
    if (be != null) {
        Binding b = be.ParentBinding;
        b.ValidationRules.Add
            (new RestrictedComboBoxItemValidationRule
                ((ComboBox)sender)); } }

これは、CodeBehindのXAMLコード(AttachedPropertyによって補完される)の代わりに、ComboBoxのバインディングにValidationRuleを追加するためにここで行うことです。これはクラスRestrictedComboBoxItemValidationRuleです(ここで監視全体が発生します):

public class RestrictedComboBoxItemValidationRule : ValidationRule {
        private DataTable itemsSource;
        private ComboBox sender;
        private bool firstChance;
        public RestrictedComboBoxItemValidationRule(ComboBox sender) {
            this.sender = sender;
            this.firstChance = true;
            this.sender.LostFocus += new RoutedEventHandler(Invalidate_OnLostFocus);
            this.itemsSource = this.GetItemsSource (sender.ItemsSource);
            if (this.itemsSource != null) {
                this.itemsSource.CaseSensitive = false; } }

        public override ValidationResult Validate(object value, CultureInfo cultureInfo) {
            ValidationResult result = ValidationResult.ValidResult;
            if (this.sender.IsReadOnly || !this.sender.IsEditable || !this.sender.IsEnabled) {
                return result; }
            if (!string.IsNullOrEmpty(value as string) && this.itemsSource != null) {
                DataRow[] r = this.itemsSource.Select
                    (this.sender.SelectedValuePath + " = '" + value.ToString() + "'");
                if (r.Length == 0 && (!this.sender.IsKeyboardFocusWithin || !this.firstChance)) {
                    result = new ValidationResult
                        (false, "The entered value is not in list!"); } }
            return result; }

        void Invalidate_OnLostFocus(object sender, RoutedEventArgs e) {
            Debug.Assert (((SWC.ComboBox)e.Source).Equals(this.sender),
                "Sender-Object invalide!");
            this.Invalidate(false); }

        /// <param name="firstChance">true, if we want to wait for LostFocus (for our ComboBox),
        /// false, if user's inputs now should be checked.
        /// null, if no update of the firstChance-flag should occur</param>
        private void Invalidate(bool? firstChance) {
            if ((this.sender.IsEditable) && (!this.sender.IsReadOnly) &&
                (!this.sender.IsKeyboardFocusWithin) && (!string.IsNullOrEmpty(this.SenderText))) {
                if (firstChance.HasValue) {
                    this.firstChance = firstChance.Value; }
                // force validation of sender-Object -> with ValidationResult Validate
                this.sender.GetBindingExpression
                    (ComboBox.TextProperty).UpdateSource(); } }

        private DataTable GetItemsSource
            (object itemsSource) {
                DataTable table = null;
            if (itemsSource.GetType() == typeof(DataTable)) {
                table = itemsSource as DataTable; 
            }
            if (itemsSource.GetType() == typeof(DataView)) {
                table = ((DataView)itemsSource).ToTable(); 
            }
            Debug.Assert(table != null,
                "Unknown source type " + itemsSource.GetType ().Name);
            return table; } 

        private string SenderText {
            get { var text = (this.sender != null) ? this.sender.Text : string.Empty;
                if (text == null) { text = string.Empty; }
                return text.ToString().Trim(); } }
    } 

私のXAMLコードは次のとおりです。

<ComboBox 
    Name="bMS_ComboBox"
    ItemsSource='{Binding Path="[BMS,ANR]"}'
    Text="{Binding [BMS]}"
    SelectedValuePath="F1"
    DisplayMemberPath="F1"
    Style="{StaticResource LocalComboBoxStyle}"
    IsEditable="True"
    c:Field.Restricted="True">

cは、AttachedPropertyが制限されたクラスフィールドの名前空間です:xmlns:c = "clr-namespace:MyAssembly.Controls; assembly = MyAssembly.Controls"

私のスタイルファイルには、次のコードがあります。

<Style x:Key="LocalComboBoxStyle" TargetType="{x:Type ComboBox}">
    <Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
            <ControlTemplate>
                <StackPanel Orientation="Horizontal">
                    <Image Source="Images/exclamation_diamond.png"
                           Height="16"
                           VerticalAlignment="Center" 
                           HorizontalAlignment="Right" 
                           Margin="0 0 2 0"/>
                    <Border BorderBrush="Red" BorderThickness="1" Opacity="0.6">
                        <Border.BitmapEffect>
                            <BlurBitmapEffect Radius="6"/>
                        </Border.BitmapEffect>
                        <AdornedElementPlaceholder/>
                    </Border>
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="True">
            <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self},
                Path=(Validation.Errors)[0].ErrorContent}"/>
        </Trigger>
    </Style.Triggers>
</Style>

それは複雑です、私は知っています、しかしそれはうまくいきます:-)

PS:ここで結果を見ることができます:ここにリンクの説明を入力してください

于 2012-11-24T09:17:14.323 に答える