如何在C#中控制依赖属性初始化的顺序

发布于 2025-01-14 20:13:55 字数 1642 浏览 3 评论 0原文

我正在尝试创建一个 TextBox 控件,该控件具有“值表”和“索引”作为依赖属性,如果我可以更改 Index 属性,并且 TextBox 将从表中查找相应的值。

代码如下:

public class Record
{
    public int Index { get; set; }
    public string Value { get; set; }
}
public class IndexedTextBox : TextBox
{
    public static readonly DependencyProperty TableProperty = DependencyProperty.Register(nameof(Table),
                    typeof(IEnumerable<Record>), typeof(IndexedTextBox));
    public static readonly DependencyProperty IndexProperty = DependencyProperty.Register(nameof(Index),
                    typeof(int), typeof(IndexedTextBox), new PropertyMetadata(0, IndexChangedCallback));

    private static void IndexChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        IndexedTextBox ctl = d as IndexedTextBox;
        int index = (int)e.NewValue;

        if (ctl.Table.Any(t => t.Index == index))
            ctl.Text = ctl.Table.Where(t => t.Index == index).FirstOrDefault().Value;
        else
            ctl.Text = "N/A";
    }

    public IEnumerable<Record> Table
    {
        get => (IEnumerable<Record>)GetValue(TableProperty);
        set => SetValue(TableProperty, value);
    }

    public int Index {
        get => (int)GetValue(IndexProperty);
        set => SetValue(IndexProperty, value);
    }
}

XAML 代码是这样的:

<local:IndexedTextBox Table="{Binding ViewModelTable}" Index="1"/>

有时有效,有时则无效。

我注意到这两个属性(表和索引)是以随机顺序初始化的,每当索引在表之前加载时,控件将无法按预期工作。

有解决方法吗?我们可以控制初始化的顺序或者我应该做的任何其他事情吗?

我非常感谢您的反馈

谢谢

I'm trying to create a TextBox control that has a "Table of values" and an "Index" as dependency properties, were I can changed the Index property and the TextBox will look for the corresponding Value from the Table.

Here's the code:

public class Record
{
    public int Index { get; set; }
    public string Value { get; set; }
}
public class IndexedTextBox : TextBox
{
    public static readonly DependencyProperty TableProperty = DependencyProperty.Register(nameof(Table),
                    typeof(IEnumerable<Record>), typeof(IndexedTextBox));
    public static readonly DependencyProperty IndexProperty = DependencyProperty.Register(nameof(Index),
                    typeof(int), typeof(IndexedTextBox), new PropertyMetadata(0, IndexChangedCallback));

    private static void IndexChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        IndexedTextBox ctl = d as IndexedTextBox;
        int index = (int)e.NewValue;

        if (ctl.Table.Any(t => t.Index == index))
            ctl.Text = ctl.Table.Where(t => t.Index == index).FirstOrDefault().Value;
        else
            ctl.Text = "N/A";
    }

    public IEnumerable<Record> Table
    {
        get => (IEnumerable<Record>)GetValue(TableProperty);
        set => SetValue(TableProperty, value);
    }

    public int Index {
        get => (int)GetValue(IndexProperty);
        set => SetValue(IndexProperty, value);
    }
}

And the XAML code is something like this:

<local:IndexedTextBox Table="{Binding ViewModelTable}" Index="1"/>

Sometimes is works, other times it won't.

I noticed that the 2 properties (Table and Index) are being initialized in random order, and whenever the Index is loaded before the Table the control won't work as expected.

Is there a workaround to that? Can we control the order of the Initialization or there any other thing I should be doing.

I'd greatly appreciate a feedback

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁把谁当真 2025-01-21 20:13:55

每当索引在表之前加载时,控件将无法按预期工作......

因此,更改实现以使其工作,例如调用两个属性的回调并在执行任何操作之前检查控件的状态,例如:

public class IndexedTextBox : TextBox
{
    public static readonly DependencyProperty TableProperty = DependencyProperty.Register(nameof(Table),
                    typeof(IEnumerable<Record>), typeof(IndexedTextBox), new PropertyMetadata(null, TableChangedCallback));

    public static readonly DependencyProperty IndexProperty = DependencyProperty.Register(nameof(Index),
                    typeof(int), typeof(IndexedTextBox), new PropertyMetadata(0, IndexChangedCallback));

    private static void TableChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) =>
        ((IndexedTextBox)d).SetText();

    private static void IndexChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) =>
        ((IndexedTextBox)d).SetText();

    private void SetText()
    {
        if (Table != null)
        {
            int index = Index;

            if (Table.Any(t => t.Index == index))
                Text = Table.Where(t => t.Index == index).FirstOrDefault().Value;
            else
                Text = "N/A";
        }
    }

    public IEnumerable<Record> Table
    {
        get => (IEnumerable<Record>)GetValue(TableProperty);
        set => SetValue(TableProperty, value);
    }

    public int Index
    {
        get => (int)GetValue(IndexProperty);
        set => SetValue(IndexProperty, value);
    }
}

whenever the Index is loaded before the Table the control won't work as expected ...

So change the implementation to make it work by for example invoking the callback for both properties and check the state of the control before doing anything, e.g.:

public class IndexedTextBox : TextBox
{
    public static readonly DependencyProperty TableProperty = DependencyProperty.Register(nameof(Table),
                    typeof(IEnumerable<Record>), typeof(IndexedTextBox), new PropertyMetadata(null, TableChangedCallback));

    public static readonly DependencyProperty IndexProperty = DependencyProperty.Register(nameof(Index),
                    typeof(int), typeof(IndexedTextBox), new PropertyMetadata(0, IndexChangedCallback));

    private static void TableChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) =>
        ((IndexedTextBox)d).SetText();

    private static void IndexChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) =>
        ((IndexedTextBox)d).SetText();

    private void SetText()
    {
        if (Table != null)
        {
            int index = Index;

            if (Table.Any(t => t.Index == index))
                Text = Table.Where(t => t.Index == index).FirstOrDefault().Value;
            else
                Text = "N/A";
        }
    }

    public IEnumerable<Record> Table
    {
        get => (IEnumerable<Record>)GetValue(TableProperty);
        set => SetValue(TableProperty, value);
    }

    public int Index
    {
        get => (int)GetValue(IndexProperty);
        set => SetValue(IndexProperty, value);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文