XAML到C#转换

时间:2014-03-12 13:21:45

标签: c# wpf xaml

<DataGridTextColumn x:Name="columnCategory" Width="10*">
                    <DataGridTextColumn.Header>
                        <ComboBox HorizontalAlignment="Left"
                            Width="{Binding ElementName=columnCategory, Path=ActualWidth, Converter={StaticResource RightMarginSetter}, ConverterParameter=20}"/>
                    </DataGridTextColumn.Header>
</DataGridTextColumn>

如何在C#中编写上述XAML表达式?

1 个答案:

答案 0 :(得分:0)

没有测试的一个例子......

        ComboBox headerCombo = new ComboBox() { HorizontalAlignment = System.Windows.HorizontalAlignment.Left };
        Binding myBinding = new Binding()
        {
            ElementName = "columnCategory",
            Path = new PropertyPath("ActualWidth"),
            Converter = new RightMarginSetter(),
            ConverterParameter = 20
        };
        headerCombo.SetBinding(ComboBox.WidthProperty, myBinding);

        DataGridTextColumn columnCategory = new DataGridTextColumn() { Header = headerCombo };