簡體   English   中英

WPF - ListView 將所選行傳入和傳出用戶控制

[英]WPF - ListView Pass Selected Row In and Out of User Control

我正在制作一個放置在 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>

我認為您的代碼中有一個循環。 lvUsers.Selection 已更改,RowSelection 已更改,lvUsers.Selection 已更改,依此類推。

    public int RowSelection
    {
        get { return lvUsers.SelectedIndex; }
        set
        {
             if(lvUsers.SelectedIndex != value)
             {
                 lvUsers.SelectedIndex = value;
             }
        }
     }

     private void lvUsers_SelectedIndexChanged(object sender, EventArgs e)
     {
         OnPropertyChanged(nameof(RowSelection));
     }

我不確定這些代碼是否有用,但您可以嘗試一下。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM