0

誰かがこのエラーに光を当てることができますか?

最初は、SelectedIndex はおそらく DependencyProperty ではなく、バインドできないと思っていましたが、間違っていました。

マークアップ拡張機能の代わりに通常のバインドを使用する場合src:ValidatedBinding、またはマークアップ拡張機能を保持して のSelectedItem代わりにバインドする場合はSelectedIndex、機能します。

これは、問題を示す小さなアプリです。

メインウィンドウ:

<Window       x:Class="WpfApplication2.MainWindow"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:src="clr-namespace:WpfApplication2"
                Title="MainWindow" 
               Height="350" 
                Width="525"
                     >
    <ComboBox SelectedIndex="{src:ValidatedBinding SelectedIndex}"
              VerticalAlignment="Center" HorizontalAlignment="Center" Width="100">
        <ComboBoxItem>Not Specified</ComboBoxItem>
        <ComboBoxItem>First</ComboBoxItem>
        <ComboBoxItem>Second</ComboBoxItem>
    </ComboBox>
</Window>

メイン ウィンドウの背後にあるコード:

using System.Windows;

namespace WpfApplication2
{
    /// <summary>
    /// The main window.
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            DataContext = new Item { Description = "Item 1", SelectedIndex = 0 };
        }
    }

    /// <summary>
    /// An object with a string and an int property.
    /// </summary>
    public sealed class Item
    {
        int _selectedIndex;
        string _description;

        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }

        public int SelectedIndex
        {
            get { return _selectedIndex; }
            set { _selectedIndex = value; }
        }
    }
}

マークアップ拡張機能のコード:

using System;
using System.Windows;
using System.Windows.Data;
using System.Windows.Markup;

namespace WpfApplication2
{
    /// <summary>
    /// Creates a normal Binding but defaults NotifyOnValidationError and 
    /// ValidatesOnExceptions to True, Mode to TwoWay and UpdateSourceTrigger
    /// to LostFocus.
    /// </summary>
    [MarkupExtensionReturnType(typeof(Binding))]
    public sealed class ValidatedBinding : MarkupExtension
    {
        public ValidatedBinding(string path)
        {
            Mode = BindingMode.TwoWay;

            UpdateSourceTrigger = UpdateSourceTrigger.LostFocus;

            Path = path;
        }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            var Target = (IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget));

            /* on combo boxes, use an immediate update and validation */
            DependencyProperty DP = Target.TargetProperty as DependencyProperty;
            if (DP != null && DP.OwnerType == typeof(System.Windows.Controls.Primitives.Selector)
                && UpdateSourceTrigger == UpdateSourceTrigger.LostFocus) {
                UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
            }

            return new Binding(Path) {
                Converter = this.Converter,
                ConverterParameter = this.ConverterParameter,
                ElementName = this.ElementName,
                FallbackValue = this.FallbackValue,
                Mode = this.Mode,
                NotifyOnValidationError = true,
                StringFormat = this.StringFormat,
                ValidatesOnExceptions = true,
                UpdateSourceTrigger = this.UpdateSourceTrigger
            };
        }

        public IValueConverter Converter { get; set; }

        public object ConverterParameter { get; set; }

        public string ElementName { get; set; }

        public object FallbackValue { get; set; }

        public BindingMode Mode { get; set; }

        [ConstructorArgument("path")]
        public string Path { get; set; }

        public string StringFormat { get; set; }

        public UpdateSourceTrigger UpdateSourceTrigger { get; set; }
    }
}

このアプリケーションを実行したときの例外:

System.Windows.Markup.XamlParseException が発生しました
HResult=-2146233087 Message= 'Set property 'System.Windows.Controls.Primitives.Selector.SelectedIndex' が例外をスローしました。' 行番号 '9' および行位置 '19'。
Source=PresentationFramework LineNumber=9 LinePosition=19
スタックトレース: System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader、IXamlObjectWriterFactory writerFactory、ブール値の skipJournaledProperties、オブジェクト rootObject、XamlObjectWriterSettings 設定、Uri baseUri) で System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader、ブール値の skipJournaledProperties、オブジェクト rootObject) 、XamlAccessLevel accessLevel、Uri baseUri) の System.Windows.Markup.XamlReader.LoadBaml (ストリーム ストリーム、ParserContext parserContext、オブジェクトの親、Boolean closeStream) の System.Windows.Application.LoadComponent (オブジェクト コンポーネント、Uri resourceLocator) の WpfApplication2.MainWindow。 c:\Users\Administrator\Documents\Visual Studio 2012\Projects\WpfApplication2\MainWindow.xaml:c の WpfApplication2.MainWindow..ctor() の 1 行目の InitializeComponent():\Users\Administrator\Documents\Visual Studio 2012\Projects\WpfApplication2\MainWindow.xaml.cs: 12 行目
InnerException: System.ArgumentException HResult=-2147024809 Message= 'System.Windows.Data.Binding' はプロパティ 'SelectedIndex' の有効な値ではありません. ソース = WindowsBase StackTrace: System.Windows.DependencyObject.SetValueCommon (DependencyProperty dp、オブジェクト値、PropertyMetadata メタデータ、ブール coerceWithDeferredReference、ブール coerceWithCurrentValue、OperationType operationType、ブール isInternal) で System.Windows.DependencyObject.SetValue (DependencyProperty dp、オブジェクト値) でSystem.Windows.Baml2006.WpfMemberInvoker.SetValue (オブジェクト インスタンス、オブジェクト値) で MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue (XamlMember メンバー、オブジェクト obj、オブジェクト値) で MS.Internal.Xaml.Runtime.ClrObjectRuntime. SetValue (オブジェクトのインスタンス、XamlMember プロパティ、オブジェクトの値) InnerException:

4

1 に答える 1

1

OK、これは誰かが興味を持っている場合に機能するプロキシ バインディングです。

正しい方向に向けてくれた@HighCoreに感謝します。

このバインディング プロキシを使用してバインディングに非標準のデフォルト値を設定するので、どこにでも設定する必要はありません。これにより、xaml がよりコンパクトになり、バインディングを「スタイル」する中心的な場所を持つことができます。

これらはデフォルトです:

  • NotifyOnValidationError = 真、
  • ValidatesOnExceptions = 真、
  • モード = 双方向、
  • UpdateSourceTrigger = 'Text' プロパティの場合は LostFocus、それ以外の場合は PropertyChanged。

対象のプロパティがプロパティでない場合はにUpdateSourceTrigger変わります。(コンボやチェックボックスなど)PropertyChangedText

検証が必要ない場合は、通常のバインディングを使用します。

<TextBlock Text="{Binding FirstName}" />

通常の双方向バインディングが必要な場合は、検証が必要になる可能性があることがわかっているため、次のバインディング プロキシを使用します。

<TextBox Text="{i:ValidatedBinding FirstName}" />

つまり、次のように書く必要はありません。

<TextBox Text="{Binding FirstName
    , Mode=TwoWay
    , UpdateSourceTrigger=LostFocus
    , NotifyOnValidationError=True
    , ValidatesOnExceptions=True" />

SelectedItem(参照型) とSelectedIndex(値型)の両方で機能します。

DataContext を監視し、バインディングを維持します。

コードの穴を見つけたり、バグを修正したり、何か提案があれば、私に知らせてください。

using ITIS.Reflection /* you can replace this with System.Reflection */;
using System;
using System.Windows;
using System.Windows.Data;
using System.Windows.Markup;

namespace ITIS
{
    /// <summary>
    /// Creates a Binding with the following defaults:
    /// <para>- NotifyOnValidationError = True, </para>
    /// <para>- ValidatesOnExceptions = True, </para>
    /// <para>- Mode = TwoWay, </para> 
    /// <para>- UpdateSourceTrigger = LostFocus for 'Text' properties, otherwise PropertyChanged.</para>
    /// </summary>
#if !SILVERLIGHT
    [MarkupExtensionReturnType(typeof(Binding))]
#endif
    public sealed class ValidatedBinding : MarkupExtension
    {
        #region CONSTRUCTOR

        public ValidatedBinding(string path)
        {
            Mode = BindingMode.TwoWay;

            Path = path;

            /* possibly changed again in ProvideValue() */
            UpdateSourceTrigger = UpdateSourceTrigger.Default;
        }

        #endregion

        #region PROPERTIES

        public IValueConverter Converter { get; set; }

        public object ConverterParameter { get; set; }

        public string ElementName { get; set; }

        public object FallbackValue { get; set; }

        public BindingMode Mode { get; set; }

#if !SILVERLIGHT
        [ConstructorArgument("path")]
#endif
        public string Path { get; set; }

        public string StringFormat { get; set; }

        public UpdateSourceTrigger UpdateSourceTrigger { get; set; }

        #endregion

        #region FIELDS

        bool _bound;
        DependencyProperty _property;
        FrameworkElement _element;

        #endregion

        #region OPERATIONS

        void ClearBinding()
        {
            _element.ClearValue(_property);
        }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            IProvideValueTarget Target = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;

            if (Target == null) {
                throw new InvalidOperationException(
                    "Cannot resolve the IProvideValueTarget. Are you binding to a property?");
            }

#if !SILVERLIGHT
            /* on text boxes, use a LostFocus update trigger */
            _property = Target.TargetProperty as DependencyProperty;

            if (_property != null) {
                if (_property.Name.StartsWith("Text")) {
                    UpdateSourceTrigger = UpdateSourceTrigger.LostFocus;
                }
                else {
                    UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
                }
            }
#endif

            _element = Target.TargetObject as FrameworkElement;

            if (_element != null) {

                _element.DataContextChanged += Element_DataContextChanged_SetBinding;

                if (_element.DataContext != null || !string.IsNullOrWhiteSpace(ElementName)) {

                    SetBinding();

                    /* can be replaced with normal reflection PropertyInfo.GetValue() */
                    return FastReflector.GetPropertyValue(/* object = */ _element.DataContext, /* property name = */ Path);
                }

                /* don't return null for value types */
                if (_property.PropertyType.IsValueType) {
                    return Activator.CreateInstance(_property.PropertyType);
                }

                return null;
            }

            return this;
        }

        void SetBinding()
        {
            _bound = true;

            Binding Binding = new Binding() {
                Path = new PropertyPath(this.Path),
                Converter = this.Converter,
                ConverterParameter = this.ConverterParameter,
                FallbackValue = this.FallbackValue,
                Mode = this.Mode,
                NotifyOnValidationError = true,
                StringFormat = this.StringFormat,
                ValidatesOnExceptions = true,
                UpdateSourceTrigger = this.UpdateSourceTrigger
            };

            /* only set when necessary to avoid a validation exception from the binding */
            if (_element.DataContext != null) { Binding.Source = _element.DataContext; }
            if (!string.IsNullOrWhiteSpace(ElementName)) { Binding.ElementName = ElementName; }

            _element.SetBinding(_property, Binding);
        }

        #endregion

        #region EVENT HANDLERS

        void Element_DataContextChanged_SetBinding(object sender, DependencyPropertyChangedEventArgs e)
        {
            /* cleanup the old binding */
            if (_bound) { ClearBinding(); }

            SetBinding();
        }

        #endregion
    }
}
于 2013-03-21T10:33:21.093 に答える