应在 WPF 中的应用程序启动时默认选择 ListView 项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12799939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:42:38  来源:igfitidea点击:

ListView Item should be selected By Default on App Launch in WPF

c#wpfxamllistviewtextbox

提问by StonedJesus

I have a listview and a textbox in my xaml file.

我的 xaml 文件中有一个列表视图和一个文本框。

View:

看法:

<ListView Grid.Column="0" ItemsSource="{Binding I2CDeviceList}" SelectedItem="{Binding SelectedI2CDeviceList, Mode=TwoWay}" Name="I2cDeviceList" >
       <ListView.View>
              <GridView>
                    <GridViewColumn Header="I2C Device" Width="Auto" DisplayMemberBinding="{Binding I2CDevName}" />
                    <GridViewColumn Header="I2C Device Address" Width="Auto" DisplayMemberBinding="{Binding I2CDeviceAddress}" />
              </GridView>
       </ListView.View>
</ListView>

<TextBox Height="23" Grid.Column="1" Name="AddressI2C" Text="{Binding Path=AddressMessage, Mode=TwoWay}" />

View Model:

查看型号:

//List View Property
public ObservableCollection<I2CModel> I2CDeviceList
    {
        get { return _I2CDeviceList; }
        set
        {
            _I2CDeviceList = value;
            NotifyPropertyChanged("I2CDeviceList");
        }
    }

    private I2CModel _selectedI2CDeviceList;
    public I2CModel SelectedI2CDeviceList
    {
        get { return _selectedI2CDeviceList; }
        set
        {
            _selectedI2CDeviceList = value;
            AddressMessage = _selectedI2CDeviceList.I2CDeviceAddress; //Displays Address in My textBox
            NotifyPropertyChanged("SelectedI2CDevSize");
        }
    }

// Property for textBox
private string _AddressMessage;
public string AddressMessage
    {
        get
        {
            return _AddressMessage;
        }
        set
        {
            _AddressMessage = value;
            NotifyPropertyChanged("AddressMessage");
        }
    }

My requirement is,

我的要求是,

When i launch the application, is it possible to have the first item of the listview selected by default? I.e. if the first item in my listview is "Chip Id", "0x03", on startup it should be selected by default and the address (0x03) must also get displayed in AddressMessage Textbox.

当我启动应用程序时,是否可以默认选择列表视图的第一项?即,如果我的列表视图中的第一项是“Chip Id”、“0x03”,则在启动时应默认选择它,并且地址 (0x03) 也必须显示在 AddressMessage 文本框中。

回答by H.B.

Set the SelectedIndexto 0in XAML.

SelectedIndex0在XAML。

回答by muhammad kashif

In your ViewModel Set SelectedI2CDeviceList to default value at the time of launch. I use ViewModel Constructor to set SelectedItem so that when my window is launched my view shows the selected value.

在您的 ViewModel 中将 SelectedI2CDeviceList 设置为启动时的默认值。我使用 ViewModel Constructor 设置 SelectedItem 以便在我的窗口启动时我的视图显示所选值。