簡體   English   中英

WPF - 為什么我的 UserControl 停止工作?

[英]WPF - Why does my UserControl stop working?

WPF - 當我取消注釋注釋行時,為什么我的UserControl停止工作? 當我評論我的控件時,它在開始時添加了行,當我取消注釋時它沒有通過LoadRow()添加任何行,甚至嘗試通過某些textboxbutton功能在運行時添加?

從自定義 UserControl 的代碼隱藏:

    public void LoadRows()
    {
        Rows.Add(new Row("221 331,44", GetOutputFromInput));
        Rows.Add(new Row("2 331,44", GetOutputFromInput));
        Rows.Add(new Row("331,44", GetOutputFromInput));
        Rows.Add(new Row("0,44", GetOutputFromInput));
        Rows.Add(new Row { Input = "333", Output = "555"});
        Rows.Add(new Row { Input = "333", Output = "555"});
    }

    //public DependencyProperty RowsProperty = DependencyProperty.Register("Rows", typeof(ObservableCollection<Row>), typeof(TriggerListAdd));
    public ObservableCollection<Row> Rows
    {
        get;
        set;
    }

僅通過取消注釋該行,您不會獲得依賴屬性。 您還必須編寫正確的 CLR 屬性包裝器:

public DependencyProperty RowsProperty = DependencyProperty.Register(
    nameof(Rows),
    typeof(ObservableCollection<Row>),
    typeof(TriggerListAdd));

public ObservableCollection<Row> Rows
{
    get { return (ObservableCollection<Row>)GetValue(RowsProperty);
    set { SetValue(RowsProperty, value);
}

我還建議將屬性的類型更改為IEnumerable以在使用控件時獲得更大的靈活性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM