誰かがこのエラーに光を当てることができますか?
最初は、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: