简体   繁体   English

如何使用XAML中的另一个自定义控件基类使WPF在视图中实例化一个自定义控件?

[英]How can I make WPF instantiate a custom control in my view, using another custom control base class in my XAML?

I have a ListView with 5 columns: 我有一个包含5列的ListView:

  <ListView x:Name="FieldList" ItemsSource="{Binding MonitorField}" SelectedItem="{Binding Field}" Margin="33,22,87,209" Grid.Column="1" Grid.RowSpan="2">
            <ListView.View>
                <GridView>
                    <GridViewColumn Width="140" Header="Field Name">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Width="127" Text="{Binding Id}" Height="32" FontSize="16" IsReadOnly="False" Background="Transparent" BorderThickness="0" TextWrapping="Wrap"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>

                    <GridViewColumn Width="140" Header="File type" >
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <ComboBox Width="127" ItemsSource="{Binding ResourceTypeValues}"  SelectedItem="{Binding ResourceTypeToLoad}" Height="24" FontSize="16" Background="Transparent" BorderThickness="0" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>

                    <GridViewColumn Width="140" Header="Path" >
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <MyNamespace:PathControl Width="127" Text="{Binding ResourcePathToLoad, Mode=TwoWay}"  Height="32" FontSize="16" Background="Transparent" TextWrapping="Wrap">
                                    <MyNamespace:PathControl.InputBindings>
                                        <MouseBinding MouseAction="LeftDoubleClick" Command="{Binding BrowseFileCommand}" />
                                    </MyNamespace:PathControl.InputBindings>
                                </MyNamespace:PathControl>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>                
</GridView>
            </ListView.View>

Here is my custom control PathControl et TestControl that inherit from PathControl 这是我的自定义控件PathControl和TestControl,它们继承自PathControl

public class PathControl : TextBox, IPathControl
{
    static PathControl()
    {
        //DefaultStyleKeyProperty.OverrideMetadata(typeof(PathControl), new FrameworkPropertyMetadata(typeof(PathControl)));
    }
}

public class TestControl : PathControl
{
    static TestControl()
    {

    }
}

What I would like to do is making WPF instantiate a custom control depending on my Combobox defining just before PathControl. 我想做的就是让WPF实例化一个自定义控件,具体取决于我在PathControl之前定义的Combobox。

For example, if I select "Txt" in the combobox, I want to create a TxtControl that inherit from PathControl. 例如,如果我在组合框中选择“ Txt”,则我想创建一个继承自PathControl的TxtControl。

The mouse binding will call a different method depending on which custom control is instantiate. 鼠标绑定将根据实例化的自定义控件调用不同的方法。

Is it even possible ? 可能吗? Is there another way to implement this ? 还有另一种方法可以实现吗?

First define as resources the two datatemplates you want like: 首先,将您想要的两个数据模板定义为资源:

<DataTemplate x:Key="case1">
 <c:PathControl />
</DataTemplate>

then the other 然后另一个

<DataTemplate x:Key="case2">
 <c:TestControl />
</DataTemplate>

Now create a DataTemplateSelector 现在创建一个DataTemplateSelector

public class SelectionTemplateSelector : DataTemplateSelector
{
public DataTemplate Case1Template { get; set; }
public DataTemplate Case2Template { get; set; }

public override DataTemplate SelectTemplate(object item, 
DependencyObject container)
{

if( //Get the binding you need)
return Case1Template ;
else
return Case2Template ;
}
}

Now add another resource: 现在添加另一个资源:

 <c:SelectionTemplateSelector 
    ImageTemplate="{StaticResource case1}" 
    StringTemplate="{StaticResource case2}" 
    x:Key="SelectionTemplateSelector " />

and finally instead adding a datatemplate add 最后改为添加一个datatemplate add

ItemTemplateSelector="{StaticResource SelectionTemplateSelector }"

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

相关问题 如何将我的自定义用户控件及其自定义视图 model 包含到我的 xaml 视图中? - How can I include my custom user control with its custom view model into my xaml view? 使用WPF自定义控件,如何通过后面的代码为自定义控件提供名称以访问它? - Using a WPF Custom Control, How can I give my custom control a name to access it via the code behind? 如何在另一个项目中使用自定义WPF / XAML控件? - How do I use a custom WPF/XAML control in another project? 在WPF中,如何实现ICommandSource以使我的自定义控制能力能够使用来自xaml的命令? - In WPF how do I implement ICommandSource to give my custom control ability to use Command from xaml? 如何将文本绑定到我的自定义XAML控件? - How to bind text to my custom XAML control? 如何在WPF自定义控件中显示基本控件 - How to display the base control in a WPF custom control WPF如何将DataTrigger应用于XAML中的“自定义用户控件”类 - WPF how to apply DataTrigger to Custom User Control class in a XAML 如何使用从主窗体上的内置控件继承的自定义控件类? - How can I use a custom control class that inherits from a built-in control on my main form? 如何在XAML WPF中访问自定义控件的属性 - how to access property of a custom control in XAML WPF 如何在WPF中进行自定义控件 - How to make custom control in WPF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM