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

时间:2015-04-29 14:16:28

标签: c# .net wpf xaml mvvm

我有一个包含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>

这是我从PathControl继承的自定义控件PathControl和TestControl

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

public class TestControl : PathControl
{
    static TestControl()
    {

    }
}

我想做的是让WPF实例化一个自定义控件,具体取决于我在PathControl之前定义的Combobox。

例如,如果我选择&#34; Txt&#34;在组合框中,我想创建一个继承自PathControl的TxtControl。

鼠标绑定将调用不同的方法,具体取决于实例化哪个自定义控件。

甚至可能吗?还有其他方法可以实现吗?

0 个答案:

没有答案