I created a UserControl with TextBlock, TextBox and ComboBox
I have MainWindow with TabControl and this tabcontrol has several UserControls, one UserControl per TabItem
The UserControls in TabItems have the custom UserContol that I created.
Everything is working fine except the ComboBox, the ComboBox fills with data items and SelectedValue, but when I switch tabs on the MainWindow the Property that's bond to the SelectedValue gets set to null value and I can not figure out what is passing the null value, I put a break point inside the ComboBox SelectedValue setter property and the value shows null when the program hits the break point, but when I try to continue stepping through the code the code exits the Set portion of the property and goes to the Get portion, it does that twice and then goes to the next Object so I can't figure out what is passing the null value. The SelectedValue gets correct value when I open the program, but gets nulls when I switch tabs.
I am new to WPF so I am not sure what I am doing.
I know it's probably impossible to tell what is causing this without seeing the entire program, but maybe someone had similar issue and has an advice on how to trouble shoot this or at least some suggestion on how to create an UserControl.
Here's the XAML to my custom UserControl
<UserControl x:Class="Vtsr.Views.CustomControls.VisitDataField"
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:customControls="clr-namespace:Vtsr.Views.CustomControls"
mc:Ignorable="d"
x:Name="VisitDataFieldControl"
d:DesignHeight="21" d:DesignWidth="300">
<UserControl.Resources>
<Style TargetType="ComboBox" x:Key="ComboBoxStyle">
<Setter Property="Height" Value="20"></Setter>
<Setter Property="FontSize" Value="{Binding FontSize}"></Setter>
</Style>
<Style TargetType="ComboBoxItem" x:Key="ComboBoxItemStyle">
<Setter Property="BorderBrush" Value="Gray"></Setter>
<Setter Property="BorderThickness" Value="0.0"></Setter>
</Style>
<Style x:Key="TextBlockStyle" TargetType="TextBlock">
<Setter Property="FontSize" Value="{Binding FontSize}"></Setter>
<Setter Property="FontWeight" Value="Bold"></Setter>
<Setter Property="Margin" Value="3"></Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="TextDecorations" Value="Underline" />
</Trigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
<Grid DataContext="{Binding ElementName=VisitDataFieldControl}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="100"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock x:Uid="TxbLabel" x:Name="DataLabel" Margin="1,1,5,1" VerticalAlignment="Center" Width="{Binding LabelWidth}"
Text="{Binding DataLabelValue}" MinWidth="20" Grid.Row="0" Grid.Column="0" Style="{StaticResource TextBlockStyle}"></TextBlock>
<customControls:FieldTextBox Text="{Binding DataField}" Grid.Row="0" Grid.Column="1" Margin="1,1,5,1" MouseRightButtonDown="TextBox_MouseRightButtonDown"></customControls:FieldTextBox>
<ComboBox ItemsSource="{Binding UnitsList}" Grid.Row="0" Grid.Column="2" Style="{StaticResource ComboBoxStyle}"
ItemContainerStyle="{StaticResource ComboBoxItemStyle}" SelectedValue="{Binding SelectedUnit, Mode=TwoWay}">
</ComboBox>
</Grid>
</UserControl>
Here's code for the custom UserControl
using System;using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using Vtsr.Model;
namespace Vtsr.Views.CustomControls
{
/// <summary>
/// Interaction logic for VisitDataField.xaml
/// </summary>
public partial class VisitDataField : INotifyPropertyChanged
{
public VisitDataField()
{
InitializeComponent();
}
public void Connect(int connectionId, object target)
{
throw new NotImplementedException();
}
public static readonly DependencyProperty LabelWidthProperty = DependencyProperty.Register("LabelWidth", typeof(int), typeof(VisitDataField), null);
public int LabelWidth
{
get
{
return (int)GetValue(LabelWidthProperty);
}
set
{
SetValue(LabelWidthProperty, value);
}
}
public static readonly DependencyProperty DataFieldProperty = DependencyProperty.Register("DataField", typeof(string), typeof(VisitDataField), null);
public string DataField
{
get { return (string)GetValue(DataFieldProperty); }
set { SetValue(DataFieldProperty, value); }
}
public static readonly DependencyProperty SelectedUnitProperty = DependencyProperty.Register("SelectedUnit", typeof(string), typeof(VisitDataField), null);
public string SelectedUnit
{
get
{
return (string)GetValue(SelectedUnitProperty);
}
set
{
SetValue(SelectedUnitProperty, value);
}
}
public string DataLabelValue
{
get
{
return _dataLabelValue;
}
set
{
_dataLabelValue = value;
if (CaptionDictinaryViewModel.CaptionDictionary != null)
_dataLabelValue = CaptionDictinaryViewModel.CaptionDictionary.ContainsKey(value) ? CaptionDictinaryViewModel.CaptionDictionary[value] : value;
OnPropertyChanged("DataLabelValue");
}
}
private string _dataLabelValue = "Data Label";
public static readonly DependencyProperty UnitsListProperty = DependencyProperty.Register("UnitsList", typeof(ObservableCollection<string>), typeof(VisitDataField), null);
public ObservableCollection<string> UnitsList
{
get { return (ObservableCollection<string>) GetValue(UnitsListProperty); }
set { SetValue(UnitsListProperty, value);}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
private void TextBox_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
}
}
}
Peter