【问题标题】:Binding XAML usercontrol in XAML在 XAML 中绑定 XAML 用户控件
【发布时间】:2024-04-26 18:10:02
【问题描述】:

我有两个用户控件:MainSettings。 在某些控件的 Main 中,我想将 XAML 中的 CommandParameter 设置为 Settings。在 C# 中代码很简单,例如:

biSettings.CommandParameter = new Settings();

如何在 XAML 中做到这一点?

CommandParameter="???"

设置有自己的模型,我正在使用 MVVM。我在这里找到了解决方案Binding UserControl in XAML,但不清楚 MVVM 因为在 VM 中是 MV! 我正在使用 Silverlight 4。

【问题讨论】:

  • 你使用的是哪个 mvvm 框架?
  • 我正在使用结合 PRISM 的 Model-View-ViewModel。

标签: silverlight xaml binding user-controls commandparameter


【解决方案1】:

假设控件是一个按钮,你可以这样:

<Button Command={Binding Foo} Content="Click Me">
  <Button.CommandParameter>
    <mystuff:Settings />
  </Button.CommandParameter>
</Button>

【讨论】:

    【解决方案2】:
      Type type = biSettings.GetType();
      FieldInfo field = type.GetField("CommandParameter");
      if (field != null)
      {
        DependencyProperty dp = (DependencyProperty)field.GetValue(Result);
        if (dp != null)
        {
          ((UserControl)Result).SetValue(dp, YourSettingObject);
    
        }
      }
    

    【讨论】:

    • 我想他说他想通过 XAML 来做
    最近更新 更多