I have created a user control as:
DogPersonControl.xaml
<UserControl x:Class="CareerChanges.Views.UserControls.DogPersonsControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:cc="clr-namespace:CareerChanges"
xmlns:local="clr-namespace:CareerChanges.Views.UserControls"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<ListView Name="lvDogPersons"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
ItemsSource="{Binding dogPersons}"
SelectedItem="{Binding Path=SelectedItem, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:DogPersonsControl}}}">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
<ListView.Resources>
<Style TargetType="{x:Type CheckBox}" BasedOn="{StaticResource {x:Type CheckBox}}">
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="IsEnabled" Value="True" />
<Setter Property="Margin" Value="4" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="VerticalContentAlignment" Value="Center" />
</Style>
</ListView.Resources>
<ListView.View>
<GridView>
<GridViewColumn Header="Name" Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource DogPersonNameConverter}">
<Binding Path="Dog.DogName"/>
<Binding Path="Person.Surname"/>
<Binding Path="Person.Given"/>
<Binding Path="ShowPerson" RelativeSource="{RelativeSource AncestorType=UserControl}"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Relationship" Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="cbDogPersonTypeList"
ItemsSource="{DynamicResource DogPersonTypes}"
SelectedValue="{Binding DogPersonTypeID, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
SelectedValuePath="DogPersonTypeID"
DisplayMemberPath="DogPersonType"
HorizontalContentAlignment="Stretch"
Width="80"
HorizontalAlignment="Left" VerticalAlignment="Stretch"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Share Phone">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid HorizontalAlignment="Stretch" >
<CheckBox IsChecked="{Binding Path=SharePhone}"/>
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Share Email">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=ShareEmail}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="CC Notified">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=CCNotified}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Adoption Declined">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=AdoptionDeclined}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</UserControl>
DogPersonControl.xaml.cs
using System.Windows;
using System.Windows.Controls;
namespace CareerChanges.Views.UserControls
{
/// <summary>
/// Interaction logic for DogPersonsControl.xaml
/// </summary>
public partial class DogPersonsControl : UserControl
{
public DogPersonsControl()
{
InitializeComponent();
}
public object SelectedItem
{
get { return GetValue(SelectedProperty); }
set { SetValue(SelectedProperty, value); }
}
public static readonly DependencyProperty SelectedProperty =
DependencyProperty.Register(
"SelectedItem",
typeof(object),
typeof(DogPersonsControl),
new FrameworkPropertyMetadata(
null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
public bool ShowPerson
{
get { return (bool)GetValue(ShowPersonProperty); }
set { SetValue(ShowPersonProperty, value); }
}
public static readonly DependencyProperty ShowPersonProperty =
DependencyProperty.Register(
"ShowPerson",
typeof(bool),
typeof(DogPersonsControl),
new FrameworkPropertyMetadata(
null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
}
}
The converter is:
public class DogPersonNameConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values[3] == System.Windows.DependencyProperty.UnsetValue)
return "";
bool ShowPerson = (bool)values[3];
if (ShowPerson)
{
string surname = (string)values[1];
string given = (string)values[2];
return string.Format("{0}, {1}", surname, given);
}
else
{
string dogName = (string)values[0];
return dogName;
}
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
The views that use it, do so as follows:
<uc:DogPersonsControl x:Name="lv" ShowPerson="True"/>
When i invoke those views, I get:
System.Windows.Markup.XamlParseException HResult=0x80131501
Message='The invocation of the constructor on type 'CareerChanges.Views.UserControls.DogPersonsControl' that matches the specified binding constraints threw an exception.' Line number '44' and line position '10'. Source=PresentationFramework StackTrace:
at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri) at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri) at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream) at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator) at CareerChanges.Views.TestWindow.InitializeComponent() in C:\Users\Tom\source\repos\CareerChanges\CareerChanges\Views\TestWindow.xaml:line 1This exception was originally thrown at this call stack: [External Code] CareerChanges.Views.UserControls.DogPersonsControl.DogPersonsControl() in DogPersonsControl.xaml.cs
Inner Exception 1: TypeInitializationException: The type initializer for 'CareerChanges.Views.UserControls.DogPersonsControl' threw an exception.
Inner Exception 2: ArgumentException: Default value type does not match type of property 'ShowPerson'.
In those cases, it failed on InitializeComponent() of the view;
I'm at a complete loss as to what the problem is.
User contributions licensed under CC BY-SA 3.0