C# XAML中的绑定问题

C# XAML中的绑定问题,c#,wpf,C#,Wpf,我正在尝试构建一个用户控件,但我在使绑定正常工作时遇到了问题。我知道我遗漏了什么,但我不知道是什么。我没有得到任何BindingExpressions XAML C#代码隐藏 namespace WpfApplication3 { /// <summary> /// Interaction logic for NumericUpDown.xaml /// </summary> public partial class NumericU

我正在尝试构建一个
用户控件
,但我在使绑定正常工作时遇到了问题。我知道我遗漏了什么,但我不知道是什么。我没有得到任何
BindingExpressions

XAML


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;
        }
    }
}
}
命名空间WpfApplication3
{
/// 
///NumericUpDown.xaml的交互逻辑
/// 
公共部分类NumericUpDown:UserControl
{
公共数字上下()
{
初始化组件();
//AnyNumericErrors=真;
}
私有字符串_NumericUpDownText;
公共字符串NumericUpDownText
{
获取{return\u NumericUpDownText;}
设置
{
_NumericUpDownText=数值;
NotifyPropertyChanged(“NumericUpDownText”);
}
}
公共事件属性更改事件处理程序属性更改;
受保护的void NotifyPropertyChanged(字符串propertyName)
{
if(PropertyChanged!=null)
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
私有无效部分\u数字上下\u丢失焦点(对象发送方,路由目标e)
{
CheckForErrors();
}
公共无效检查错误()
{
尝试
{
int value=Int32.Parse(NumericUpDownText);
AnyNumericErrors=false;
}
捕获(格式化异常)
{
Debug.WriteLine(“错误”);
AnyNumericErrors=真;
}
}
私有布尔m_AnyNumericErrors;
公共布尔AnyNumericErrors
{
得到
{
返回m_AnyNumericErrors;
}
设置
{
m_AnyNumericErrors=数值;
NotifyPropertyChanged(“任何数字错误”);
}
}
#区域DP
公共Int32 LowerBound
{
得到;
设置
}
public static readonly dependencProperty LowerBoundProperty=dependencProperty.Register(“LowerBound”、typeof(Int32)、typeof(NumericUpDown));
#端区
私有无效部分\u增加按钮\u单击(对象发送方,路由目标)
{
尝试
{
Int32 value=Int32.Parse(NumericUpDownText);
值++;
NumericUpDownText=value.ToString();
}
捕获(例外)
{
AnyNumericErrors=真;
}
}
私有无效部分\u递减按钮\u单击(对象发送方,路由目标)
{
尝试
{
Int32 value=Int32.Parse(NumericUpDownText);
价值--;
NumericUpDownText=value.ToString();
}
捕获(例外)
{
AnyNumericErrors=真;
}
}
}
}
编辑


主要问题是DataTriggers不工作。。。在开始时,文本框是蓝色的,但从不改变。当我按下向上/向下键以递增/递减值时,会调用事件,但UI中的值不会改变。您应该使用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));

如果这还不够,请删除祖先搜索的级别限制。

我尝试将DataContext设置为该值,但仍然无法使用“PresentationTraceSources”调试绑定。在触发器中更改除背景(如FontSize)以外的任何内容,然后查看它是否仍然没有响应。
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));