私は、少し複雑な方法を見つけました。しかし、それは機能します!これが方法です:
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:ここで結果を見ることができます:ここにリンクの説明を入力してください