C# 如何允许对自定义控件DependencyProperty的内部属性进行只读绑定?

C# 如何允许对自定义控件DependencyProperty的内部属性进行只读绑定?,c#,wpf,binding,dependency-properties,C#,Wpf,Binding,Dependency Properties,我正在开发一个CustomControl,它公开了dependencPropertySearchRange,它基于自定义类范围 public class MyCustomControl : Control { public static readonly DependencyProperty SearchRangeProperty = DependencyProperty.Register( "SearchRange", t

我正在开发一个
CustomControl
,它公开了
dependencProperty
SearchRange
,它基于自定义类
范围

public class MyCustomControl : Control
{
    public static readonly DependencyProperty SearchRangeProperty
        = DependencyProperty.Register(
            "SearchRange",
            typeof (Range<DateTime>),
            typeof (VariableBrowser));

    // ...

    public Range<DateTime> SearchRange
    {
        get { return (Range<DateTime>)this.GetValue(SearchRangeProperty); }
        set { this.SetValue(SearchRangeProperty, value); }
    }

    // ...
}
我遵循的规范要求使用我的自定义控件的应用程序只能绑定到
SearchRange
属性,以便读取其内部值(
最小值
最大值
),因为这些值必须在内部处理,并且只能由我的
自定义控件设置。对
SearchRange
属性或其内部道具(
Minimum
Maximum
)进行任何更改后,应更新绑定目标,而无需重新分配整个
SearchRange
。 或者,我应该允许直接绑定到内部属性(
SearchRange.Minimum
SearchRange.Maximum

我尝试了许多不同的方法来实现这个结果,但没有一个成功。我怎样才能获得所需的结果


提前感谢。

最小值和最大值应该有两个依赖属性,注册a。在这个回调中,您可以从新值构造一个范围,并在range属性上使用(这将保持绑定不变)。还可以为range属性提供回调,在该属性中使用SetCurrentValue更新其他两个属性

伪代码:

private static void MinChangedCallback(DependencyObject o, TheRightKindOfArgs e)
{
    var control = (MyCustomControl)o;
    control.UpdateRange((DateTime)e.NewValue, Maximum);
}
private static void MaxChangedCallback(DependencyObject o, TheRightKindOfArgs e)
{
    var control = (MyCustomControl)o;
    control.UpdateRange(Minimum, (DateTime)e.NewValue);
}

private void UpdateRange(DateTime min, DateTime max)
{
    var range = new Range<DateTime>(min, max);
    SetCurrentValue(SearchRangeProperty, range);
}
private static void MinChangedCallback(DependencyObject o,TheRightKindOfArgs e)
{
var-control=(MyCustomControl)o;
control.UpdateRange((DateTime)e.NewValue,最大值);
}
私有静态void MaxChangedCallback(DependencyObject o,TheRightKindOfArgs e)
{
var-control=(MyCustomControl)o;
control.UpdateRange(最小值,(日期时间)e.NewValue);
}
私有void UpdateRange(日期时间最小值,日期时间最大值)
{
var范围=新范围(最小值、最大值);
SetCurrentValue(SearchRangeProperty,范围);
}

您尝试过什么?您应该能够直接绑定到这些属性,就像绑定到任何其他属性一样;您是否遇到了特定的问题,或者您不知道如何使用数据绑定?
private static void MinChangedCallback(DependencyObject o, TheRightKindOfArgs e)
{
    var control = (MyCustomControl)o;
    control.UpdateRange((DateTime)e.NewValue, Maximum);
}
private static void MaxChangedCallback(DependencyObject o, TheRightKindOfArgs e)
{
    var control = (MyCustomControl)o;
    control.UpdateRange(Minimum, (DateTime)e.NewValue);
}

private void UpdateRange(DateTime min, DateTime max)
{
    var range = new Range<DateTime>(min, max);
    SetCurrentValue(SearchRangeProperty, range);
}