简体   繁体   English

如何将XAML(ControlTemplate)转换为Code-Behind

[英]How to convert XAML(ControlTemplate) to Code-Behind

<dxg:LookUpEdit Margin="5" HorizontalAlignment="Stretch" VerticalAlignment="Center" 
                    ItemsSource="{Binding}"
                    DisplayMember="Name" 
                    AutoPopulateColumns="False">
    <dxg:LookUpEdit.PopupContentTemplate>
        <ControlTemplate>
            <dxg:GridControl Name="PART_GridControl">
                <dxg:GridControl.Columns>
                    <dxg:GridColumn FieldName="Name"/>
                    <dxg:GridColumn FieldName="Date"/>
                </dxg:GridControl.Columns>
            </dxg:GridControl>
        </ControlTemplate>
    </dxg:LookUpEdit.PopupContentTemplate>
</dxg:LookUpEdit>

please help me, I do not know how to add GridColumn on GridControl and set LookUpEdit.PopupContentTemplate = GridControl 请帮助我,我不知道如何在GridControl上添加GridColumn并设置LookUpEdit.PopupContentTemplate = GridControl

Thank you very much. 非常感谢你。 I think XAMLREADER can help me, I'm stuck here 我认为XAMLREADER可以帮助我,我被困在这里

ControlTemplate ct = new ControlTemplate();
FrameworkElementFactory gridcontrol = new FrameworkElementFactory(typeof(GridControl));
FrameworkElementFactory gridcolumn = new FrameworkElementFactory(typeof(GridColumn));
gridcontrol.SetValue(?, ?);
gridcolumn.SetValue(?, ?);
ct.VisualTree = gridcontrol;

creating control/data templates in code behind involves factory classes, that may be more complicated for non framework types. 在后面的代码中创建控件/数据模板涉及工厂类,这对于非框架类型而言可能更为复杂。

example from Creating a control template with code behind 创建带有代码的控件模板的示例

ControlTemplate template = new ControlTemplate(typeof(ListBoxItem));
FrameworkElementFactory elemFactory = new FrameworkElementFactory(typeof(Border));
elemFactory.Name = "Border";
elemFactory.SetValue(Border.CornerRadiusProperty, new CornerRadius(5));
elemFactory.SetValue(Border.PaddingProperty, new Thickness(1));
elemFactory.SetValue(Border.SnapsToDevicePixelsProperty, true);
template.VisualTree = elemFactory;

//same can be used as 
LookUpEdit.PopupContentTemplate = template;

above is just an example, you may need to use the appropriate types 以上仅是示例,您可能需要使用适当的类型

alternatively keeping the control templates in resource dictionary may help you 或者将控制模板保存在资源字典中可能对您有帮助

or the easiest solution is to parse the xaml in code behind 或最简单的解决方案是在后面的代码中解析xaml

const string xaml = "<ControlTemplate><dxg:GridControl Name=""PART_GridControl""><dxg:GridControl.Columns><dxg:GridColumn FieldName=""Name""/><dxg:GridColumn FieldName=""Date""/></dxg:GridControl.Columns></dxg:GridControl></ControlTemplate>";
ControlTemplate template = XamlReader.Parse(xaml) as ControlTemplate;

//this can be used as 
LookUpEdit.PopupContentTemplate = template;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM