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 回答