如何在C#-code中动态设置DataContext?

时间:2021-07-24 13:17:33

I builded a RibbonGroupBox like this in a C# file:

我在C#文件中构建了一个这样的RibbonGroupBox:

public class TextControl : RibbonGroupBox
{
    public TextControl()
    {
        const double widthOfComboBoxes = 150;

        Binding fontsBinding = new Binding();
        fontsBinding.Source = (TextControlVM)DataContext;
        fontsBinding.Path = new System.Windows.PropertyPath("Fonts");
        fontsBinding.Mode = BindingMode.TwoWay;

        Binding fontSizeBinding = new Binding();
        fontSizeBinding.Source = (TextControlVM)DataContext;
        fontSizeBinding.Path = new System.Windows.PropertyPath("FontSize");
        fontSizeBinding.Mode = BindingMode.TwoWay;

        /* Combobox for the fonts (Arial, etc.) */
        Fluent.ComboBox fontCombo = new Fluent.ComboBox();
        fontCombo.SetBinding(Fluent.ComboBox.ItemsSourceProperty, fontsBinding);
        fontCombo.SelectedItem = ((TextControlVM)DataContext).DefaultFont;
        fontCombo.Width = widthOfComboBoxes;
        this.AddChild(fontCombo);

        /* Combobox for the fontsizes */
        Fluent.ComboBox fontSizeCombo = new Fluent.ComboBox();
        fontSizeCombo.SetBinding(Fluent.ComboBox.ItemsSourceProperty, fontSizeBinding);
        fontSizeCombo.SelectedItem = ((TextControlVM)DataContext).DefaultFontSize;
        fontSizeCombo.Width = widthOfComboBoxes;
        this.AddChild(fontSizeCombo);
    }
}

I furthermore have a viewmodel (TextControlVM) that contains Properties for Fonts, FontSize, DefaultFont and DefaultFontSize.

我还有一个viewmodel(TextControlVM),它包含Fonts,FontSize,DefaultFont和DefaultFontSize的属性。

When I now use this in another module like this, the DataContext in the above example is null:

当我现在在这样的另一个模块中使用它时,上面示例中的DataContext为null:

<Fluent:RibbonTabItem Header="Export">
    <TextControl DataContext="{Binding DataContext.TextControl}"/>
</Fluent:RibbonTabItem>

When I build the RibbonGroupBox with XAML code everything works fine, so I want to do what XAML automatically does. How can I do that?

当我使用XAML代码构建RibbonGroupBox时,一切正常,所以我想做XAML自动执行的操作。我怎样才能做到这一点?

Background: I want to use the RibbonGroupBox in several modules. That is why I build it with C#-Code, so that I can access it dynamically. The DataContext will change dependend on the call.

背景:我想在几个模块中使用RibbonGroupBox。这就是为什么我用C#-Code构建它,以便我可以动态访问它。 DataContext将根据调用进行更改。

1 个解决方案

#1


0  

The DataContext is implied in a binding automatically, so you are essentially binding to RibbonTabItem.DataContext.DataContext.TextControl, which doesn't exist

DataContext自动隐含在绑定中,因此您实际上是绑定到RibbonTabItem.DataContext.DataContext.TextControl,它不存在

To bind to RibbonTabItem.DataContext.TextControl, simply leave the extra DataContext out of the binding

要绑定到RibbonTabItem.DataContext.TextControl,只需将额外的DataContext保留在绑定之外

<Fluent:RibbonTabItem Header="Export">
    <TextControl DataContext="{Binding TextControl}"/>
</Fluent:RibbonTabItem>

#1


0  

The DataContext is implied in a binding automatically, so you are essentially binding to RibbonTabItem.DataContext.DataContext.TextControl, which doesn't exist

DataContext自动隐含在绑定中,因此您实际上是绑定到RibbonTabItem.DataContext.DataContext.TextControl,它不存在

To bind to RibbonTabItem.DataContext.TextControl, simply leave the extra DataContext out of the binding

要绑定到RibbonTabItem.DataContext.TextControl,只需将额外的DataContext保留在绑定之外

<Fluent:RibbonTabItem Header="Export">
    <TextControl DataContext="{Binding TextControl}"/>
</Fluent:RibbonTabItem>