DataBindingは、グループ内で9を超えるRadioButtonグループを使用するすべてのバインディングに適した行をリスト内で見つけることができません

StackOverflow https://stackoverflow.com//questions/23051112

  •  21-12-2019
  •  | 
  •  

質問

私は説明された技術を使用しています ここに 私のラジオボタンのグループを私のC#winformsアプリケーションに追加する。

このテクニックは、9より大きいタグのラジオボタンを試すまで美しく機能します。

この場合、ラジオボタンをクリックするとエラーが発生します

システム。InvalidOperationExceptionが処理されませんでしたHResult=-2146233079
Message=DataBindingは、リスト内で適切な行を見つけることができません すべてのバインディング。ソース=システムスタックトレース:システムで。コンポーネントモデル。ReflectPropertyDescriptor.SetValue(オブジェクト コンポーネント、オブジェクト値) システムで。窓。フォーム。BindToObject。SetValue(オブジェクト値) システムで。窓。フォーム。ビンディング。PullData(Boolean reformat,Boolean force) システムで。窓。フォーム。ビンディング。Target_propertychanged(オブジェクト送信者,EventArgs e) システムで。イヴェント-ハンドラーInvoke(オブジェクト送信者,EventArgs e) SBDでビブスナップウイRadioGroupBox。radiobutton_checkedchanged(オブジェクト送信者, EventArgs e)で e:\EShared\devnet10\VivSnap\SnapInUI adioGroupBox...cs:ライン70 システムで。イヴェント-ハンドラーInvoke(オブジェクト送信者,EventArgs e) システムで。窓。フォーム。ラジオボタンOnClick(EventArgs e) システムで。窓。フォーム。ラジオボタンOnMouseUp(MouseEventArgs mevent) システムで。窓。フォーム。コントロール。WmMouseUp(メッセージ&m,MouseButtonsボタン,Int32クリック) システムで。窓。フォーム。コントロール。WndProc(メッセージ&m) システムで。窓。フォーム。ボタンベース。WndProc(メッセージ&m) システムで。窓。フォーム。NativeWindow。デバッグ可能コールバック(IntPtr hWnd,Int32msg,IntPtr wparam,IntPtr lparam) システムで。窓。フォーム。不衛生な方法だディスパッチメッセージ(MSG&msg) システムで。窓。フォーム。アプリケーション。コンポーネントマネージャ。システム。窓。フォーム。不衛生な方法だIMsoComponentManagerです。FPushMessageLoop(IntPtr dwComponentID、Int32理由、Int32pvLoopData) システムで。窓。フォーム。アプリケーション。ThreadContext。RunMessageLoopInner(Int32 理由、ApplicationContextコンテキスト) システムで。窓。フォーム。アプリケーション。ThreadContext。RunMessageLoop(Int32 理由、ApplicationContextコンテキスト) システムで。窓。フォーム。フォーム。ShowDialog(Iwin32Windowの所有者) SBDでビブスナップメイン...フォーム1。btnform1go_click(オブジェクト送信者,EventArgs e)in e:\EShared\devnet10\VivSnap\Main\Form1...cs:36行目 システムで。窓。フォーム。ボタン。OnMouseUp(MouseEventArgs mevent) システムで。窓。フォーム。コントロール。WmMouseUp(メッセージ&m,MouseButtonsボタン,Int32クリック) システムで。窓。フォーム。コントロール。WndProc(メッセージ&m) システムで。窓。フォーム。ボタンベース。WndProc(メッセージ&m) システムで。窓。フォーム。ボタン。WndProc(メッセージ&m) システムで。窓。フォーム。NativeWindow。デバッグ可能コールバック(IntPtr hWnd,Int32msg,IntPtr wparam,IntPtr lparam) システムで。窓。フォーム。不衛生な方法だディスパッチメッセージ(MSG&msg) システムで。窓。フォーム。アプリケーション。コンポーネントマネージャ。システム。窓。フォーム。不衛生な方法だIMsoComponentManagerです。FPushMessageLoop(IntPtr dwComponentID、Int32理由、Int32pvLoopData) システムで。窓。フォーム。アプリケーション。ThreadContext。RunMessageLoopInner(Int32 理由、ApplicationContextコンテキスト) システムで。窓。フォーム。アプリケーション。ThreadContext。RunMessageLoop(Int32 理由、ApplicationContextコンテキスト) SBDでビブスナップメイン...プログラム。メイン()でe:\EShared\devnet10\VivSnap\Main\Program...cs:18行目 システムで。AppDomain。_Executeassembly(RuntimeAssemblyアセンブリ,String[]args) マイクロソフトで。ビジュアルトゥーディオホストプロセス。ホストプロックRunUsersAssembly() システムで。スレッディング実行コンテキスト。RunInternal(ExecutionContext executionContext,ContextCallbackコールバック,オブジェクト状態,ブール値 保存-保存) システムで。スレッディング実行コンテキスト。Run(ExecutionContext executionContext,ContextCallbackコールバック,オブジェクト状態,ブール値 保存-保存) システムで。スレッディング実行コンテキスト。Run(ExecutionContext executionContext,ContextCallbackコールバック,オブジェクト状態) システムで。スレッディングThreadHelper。ThreadStart()InnerException:

プロパティをバインドするための私のコードは

groupBox.DataBindings.Add("Selected", dataSource, PrinterTypeNum, false, DataSourceUpdateMode.OnPropertyChanged);

プロパティの私のコードは

public int PrinterTypeNum  
{
    get
    {
        try
        {
            return (int)this.PrinterType;
        }
        catch (Exception)
        {
            return 0;
        }
    }
    set
    {
        try
        {
            this.PrinterType = (jtVivPrinterEnum)value;
            // the enum goes from 0 to 15
        }
        catch (Exception)
        {
            this.PrinterType = jtVivPrinterEnum.jtVivPrinterUnknown;
            throw;
        }
    }
}

public jtVivPrinterEnum PrinterType { get; set; }

**

[更新]プロパティが次のようになっている場合、エラーが発生しないことを発見しました。

public int PrinterTypeNum { get; set; }

私のラジオグループクラスは次のとおりです //WinFormsでradiobuttonのグループをデータバインドする最良の方法

public partial class RadioGroupBox : GroupBox
{
    public RadioGroupBox()
    {
        this.InitializeComponent();
    }

    public event EventHandler SelectedChanged = delegate { };
    int _selected;
    public int Selected
    {
        get
        {
            return this._selected;
        }
        set
        {
            int val = 0;
            var radioButton = this.Controls.OfType<RadioButton>()
            .FirstOrDefault(radio => radio.Tag != null
            && int.TryParse(radio.Tag.ToString(), out val) && val == value);

            if (radioButton != null)
            {
                try
                {
                    radioButton.Checked = true;
                    this._selected = val;
                }
                catch (Exception ex)
                {
                    Debug.Print(ex.ToString());
                    throw;
                }
            }
        }
    }

    protected override void OnControlAdded(ControlEventArgs e)
    {
        base.OnControlAdded(e);
        var radioButton = e.Control as RadioButton;
        if (radioButton != null)
            radioButton.CheckedChanged += this.radioButton_CheckedChanged;
    }

    void radioButton_CheckedChanged(object sender, EventArgs e)
    {
        var radio = (RadioButton)sender;
        int val = 0;
        if (radio.Checked && radio.Tag != null
        && int.TryParse(radio.Tag.ToString(), out val))
        {
            this._selected = val;
            this.SelectedChanged(this, new EventArgs());  // raises error when val=10
        }
    }
}
役に立ちましたか?

解決

私が変更したときにこのエラーが発生しました .Visible データバインドコンポーネントのいずれかでfalseにプロパティ。

回避策は、コントロールを変更する前にコントロールのDataBindingsを削除することです .Visible プロパティをfalseにしてから、DataBindingsを再度表示するときにDataBindingsを追加します。

エラーとその回避方法を説明するコードは次のとおりです:

void SetupStuff()
{
    BindingSource myBindingSource = new BindingSource();
    // Retrieve my data into a DataTable and add it to the BindingSource
    DataTable myDataTable = GetMyDataTable(); 
    myBindingSource.DataSource = myDataTable.DefaultView;

    // Add DataBindings to Visible components only
    myTextBox.Visible = true;
    myTextBox.DataBindings.Add("Text", myBindingSource, "MySecret");
}

void UpdateStuff()
{
    // Update the currently selected data record
    myDataTable.DefaultView[myBindingSource.Position].BeginEdit();
    myDataTable.DefaultView[myBindingSource.Position]["MySecret"] = "Top Secret Meetup " + DateTime.Now.AddDays(1).ToString();
    myDataTable.DefaultView[myBindingSource.Position].EndEdit();
    myDataTable.DefaultView[myBindingSource.Position].AcceptChanges();
    // if myTextBox.Visible, then we can see the message
}

void HideStuff()
{
    myTextBox.Visible = false;
    UpdateStuff();

    // After myDataTable..AcceptChanges(), DataBindings are now BROKEN!
    // This Cleared all data from controls bound to myBindingSource 
    try
    {
        myBindingSource.ResetCurrent();
    }
    catch (Exception brokenBindingSource)
    {
        Console.WriteLine("DataBinding broken" + brokenBindingSource.Message);
    }
}

void HideStuff()
{
    if (myTextBox.Visible)
    {
        myTextBox.DataBindings.Remove("MySecret");
        myTextBox.Visible = false;
    }
    UpdateStuff();

    // After myDataTable..AcceptChanges(), DataBindings are now BROKEN!
    // This Cleared all data from controls bound to myBindingSource 
    try
    {
        myBindingSource.ResetCurrent();
    }
    catch (Exception brokenBiSo)
    {
        Console.WriteLine("DataBinding is broken" + brokenBiSo.Message);
    }
}

void ShowStuff()
{
    // If I want to make the component visible again, do this:
    if (!myTextBox.Visible)
    {
        myTextBox.Visible = true;
        myTextBox.DataBindings.Add("Text", myBindingSource, "MySecret");
    }
}

他のヒント

問題は、選択された値に依存している別のプロパティを持っていたためです。 machineプロパティのタイプがPrintTypeプロパティ

に依存するバインドプロパティとして "machine.mode"を持っていました

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top