Я делаю пользовательский элемент управления для размещения в приложении SCADA. Я выставляю свойство RowSelection. Когда свойство RowSelection изменяется, я обновляю выбранный индекс в моем списке просмотра (это работает). Но я также хочу обновить свойство, когда выбранная строка изменяется пользователем, щелкающим в просмотре списка. Мое событие запускается, но свойство не обновляется или не передается из пользовательского элемента управления. Любые решения?
namespace RowSelection
{
public partial class UserControl1 : UserControl, INotifyPropertyChanged
{
private System.Collections.ObjectModel.ObservableCollection<User> users = new System.Collections.ObjectModel.ObservableCollection<User>()
{
new User() { Name = "User 1", Age = 42 },
new User() { Name = "User 2", Age = 19 },
new User() { Name = "User 3", Age = 65 },
};
public UserControl1()
{
InitializeComponent();
lvUsers.ItemsSource = users;
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public int RowSelection
{
get { return _rowSelection; }
set
{
_rowSelection = value;
lvUsers.SelectedIndex = RowSelection;
OnPropertyChanged("RowSelection");
}
}
private int _rowSelection;
private void lvUsers_SelectedIndexChanged(object sender, EventArgs e)
{
RowSelection = lvUsers.SelectedIndex;
//==============================================================================================================================================
//This is where the RowSelection property should be set when a row is selected by clicking inside the User Control
//I know this event is firing but it is not successfully updating the parameter or the parameter value is not being passed outside the control
//==============================================================================================================================================
}
public class User : INotifyPropertyChanged
{
public string Name { get; set; }
public int Age { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
<UserControl x:Class="RowSelection.UserControl1"
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:local="clr-namespace:RowSelection"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid OpacityMask="#FFDECCCC" Background="White" x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<ListView Margin="5" Name="lvUsers" SelectionMode="Single" SelectionChanged="lvUsers_SelectedIndexChanged">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" Width="120">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" HorizontalAlignment="Center" Foreground="Black"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Age" Width="120">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Age}" HorizontalAlignment="Center" Foreground="Black"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>