I have a ListView with 5 columns:
<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
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.
For example, if I select "Txt" in the combobox, I want to create a TxtControl that inherit from PathControl.
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
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
ItemTemplateSelector="{StaticResource SelectionTemplateSelector }"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.