0

をビルドしようとしてUserControlいますが、バインディングが機能しません。何かが欠けていることは知っていますが、それが何であるかわかりません。私は何も得ていませんBindingExpressions

XAML

<UserControl x:Class="WpfApplication3.NumericUpDown"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfApplication3">
<Grid>
    <StackPanel>
        <TextBox Text="{Binding NumericUpDownText, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type local:NumericUpDown}}}" LostFocus="PART_NumericUpDown_LostFocus">
            <TextBox.Resources>
                <Style TargetType="TextBox">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding AnyNumericErrors, RelativeSource={RelativeSource AncestorType={x:Type local:NumericUpDown}, AncestorLevel=1}}" Value="false">
                            <Setter Property="Background" Value="Blue" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding AnyNumericErrors, RelativeSource={RelativeSource AncestorType={x:Type local:NumericUpDown}, AncestorLevel=1}}" Value="true">
                            <Setter Property="Background" Value="Red" />
                            <Setter Property="ToolTip" Value="There is an error" />
                        </DataTrigger>
                    </Style.Triggers>

                    <EventSetter Event="LostFocus" Handler="PART_NumericUpDown_LostFocus" />
                </Style>
            </TextBox.Resources>
            <TextBox.Template>
                <ControlTemplate>
                    <Border Background="{TemplateBinding Background}"
                              BorderBrush="{TemplateBinding BorderBrush}"
                              BorderThickness="{TemplateBinding BorderThickness}">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>

                            <ScrollViewer x:Name="PART_ContentHost" 
                                              Grid.Column="0" />

                            <Grid Grid.Column="1">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="*" />
                                    <RowDefinition Height="*" />
                                </Grid.RowDefinitions>

                                <RepeatButton x:Name="PART_IncreaseButton"
                                              Click="PART_IncreaseButton_Click" 
                                              Content="UP"  />
                                <RepeatButton x:Name="PART_DecreaseButton"
                                              Grid.Row="1"
                                              Click="PART_DecreaseButton_Click"
                                              Content="Down"/>
                            </Grid>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </TextBox.Template>
        </TextBox>
    </StackPanel>
</Grid>

C# コード ビハインド

namespace WpfApplication3
{
    /// <summary>
    /// Interaction logic for NumericUpDown.xaml
    /// </summary>
    public partial class NumericUpDown : UserControl
    {
    public NumericUpDown()
    {
        InitializeComponent();
        //AnyNumericErrors = true;
    }

    private String _NumericUpDownText;
    public String NumericUpDownText
    {
        get { return _NumericUpDownText; }
        set
        {
            _NumericUpDownText = value;
            NotifyPropertyChanged("NumericUpDownText");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void NotifyPropertyChanged(String propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    private void PART_NumericUpDown_LostFocus(object sender, RoutedEventArgs e)
    {
        CheckForErrors();   
    }

    public void CheckForErrors()
    {
        try
        {
            int value = Int32.Parse(NumericUpDownText);
            AnyNumericErrors = false;
        }
        catch (FormatException)
        {
            Debug.WriteLine("error");
            AnyNumericErrors = true;
        }
    }

    private Boolean m_AnyNumericErrors;
    public Boolean AnyNumericErrors
    {
        get 
        {
            return m_AnyNumericErrors; 
        }
        set
        {
            m_AnyNumericErrors = value;
            NotifyPropertyChanged("AnyNumericErrors");
        }
    }

    #region DP

    public Int32 LowerBound
    {
        get;
        set;
    }

    public static readonly DependencyProperty LowerBoundProperty = DependencyProperty.Register("LowerBound", typeof(Int32), typeof(NumericUpDown));

    #endregion

    private void PART_IncreaseButton_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            Int32 value = Int32.Parse(NumericUpDownText);
            value++;
            NumericUpDownText = value.ToString();
        }
        catch (Exception)
        {
            AnyNumericErrors = true;
        }
    }

    private void PART_DecreaseButton_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            Int32 value = Int32.Parse(NumericUpDownText);
            value--;
            NumericUpDownText = value.ToString();
        }
        catch (Exception)
        {
            AnyNumericErrors = true;
        }
    }
}
}

編集

主な問題は、DataTriggers が機能していないことです... 最初は、テキスト ボックスは青色ですが、変化しません。そして、値を増減するために上/下を押すと、イベントが呼び出されますが、値はUIで変化しません

4

1 に答える 1

1

次のような DependencyProperties を使用する必要があります。

public bool AnyNumericErrors
{
  get { return (bool)GetValue(AnyNumericErrorsProperty); }
  set { SetValue(AnyNumericErrorsProperty, value); }
}

// Using a DependencyProperty as the backing store for AnyNumericErrors.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty AnyNumericErrorsProperty =
    DependencyProperty.Register("AnyNumericErrors", typeof(bool), typeof(NumericUpDown), new UIPropertyMetadata(false));

それでも不十分な場合は、先祖検索のレベル制限を解除してください。

于 2012-10-18T14:42:15.707 に答える