I am creating a simple User Control combining a popup with a text view, nothing crazy. When I first set it up in a window to completely style it, it worked fine, but when I moved it to User Control to finish it, it wonโt work correctly.
I pass the value of min and max to the control, and then automatically creates a list of numbers to select from this range. In the user element, the list of numbers is not anchored correctly, who knows why. Maybe someone can take a look at my code.
I read a bunch of other questions about this, but I don't know what is going on. I am not getting any errors in my output window, so there are no hints. Anyway, here is the code -
UserControl.xaml
<UserControl x:Class="UserControl1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="Me" DataContext="{Binding RelativeSource={RelativeSource Self}}"> <StackPanel> <TextBox Name="myTextbox" Height="30" Margin="0" FontSize="14" IsReadOnly="True" Padding="5,2" Text="{Binding Value}" /> <Popup x:Name="myPopup" PlacementTarget="{Binding ElementName=myTextbox}" StaysOpen="True"> <Popup.Style> <Style TargetType="{x:Type Popup}"> <Style.Triggers> <DataTrigger Binding="{Binding ElementName=myTextbox, Path=IsFocused}" Value="True"> <Setter Property="IsOpen" Value="True" /> </DataTrigger> </Style.Triggers> </Style> </Popup.Style> <StackPanel> <ListView Name="myListView" Height="100" MaxHeight="300" ItemsSource="{Binding List, UpdateSourceTrigger=PropertyChanged}" SelectionChanged="ListView_SelectionChanged"> <ListView.ItemTemplate> <DataTemplate> <Label Width="100" Height="30" Margin="0" Content="{Binding}" FontFamily="Segoe UI" FontSize="14" Padding="5,2" /> </DataTemplate> </ListView.ItemTemplate> </ListView> <Button Width="200" Height="30" /> </StackPanel> </Popup> </StackPanel>
UserControl.xaml.vb
Imports System.ComponentModel Imports System.Linq.Expressions Imports System.Collections.ObjectModel Class UserControl1 ' Dependency Properties Public Shared ReadOnly ListProperty As DependencyProperty = DependencyProperty.Register("List", GetType(ObservableCollection(Of Integer)), GetType(MainWindow)) Public Shared ReadOnly ValueProperty As DependencyProperty = DependencyProperty.Register("Value", GetType(Integer), GetType(MainWindow)) Public Shared ReadOnly MaxValueProperty As DependencyProperty = DependencyProperty.Register("MaxValue", GetType(Integer), GetType(MainWindow)) Public Shared ReadOnly MinValueProperty As DependencyProperty = DependencyProperty.Register("MinValue", GetType(Integer), GetType(MainWindow)) ' Properties Public Property List As ObservableCollection(Of Integer) Get Return DirectCast(GetValue(ListProperty), ObservableCollection(Of Integer)) End Get Set(value As ObservableCollection(Of Integer)) SetValue(ListProperty, value) End Set End Property Public Property Value As Integer Get Return DirectCast(GetValue(ValueProperty), Integer) End Get Set(value As Integer) SetValue(ValueProperty, value) End Set End Property Public Property MaxValue As Integer Get Return DirectCast(GetValue(MaxValueProperty), Integer) End Get Set(value As Integer) SetValue(MaxValueProperty, value) End Set End Property Public Property MinValue As Integer Get Return DirectCast(GetValue(MinValueProperty), Integer) End Get Set(value As Integer) SetValue(MinValueProperty, value) End Set End Property Private Sub ListView_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs) Value = List(myListView.SelectedIndex) End Sub Private Sub UserControl1_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Loaded List = New ObservableCollection(Of Integer) ' Add all available numbers into the list For iCounter As Integer = MinValue To MaxValue List.Add(iCounter) Next ' Set the selected index on the list for the value myListView.SelectedIndex = Value - MinValue End Sub End Class
Just for reference, when I checked this, I set the Min and Max values โโboth in the window settings and in the usercontrol setting.