簡體   English   中英

如何在MVVM-WPF中獲取所選項目

[英]How to get selected item in MVVM-WPF

嗨我正在使用WPF和MVVM,我的應用程序中有“編輯/更新”面板,它更新了Grid中的選定數據。 它工作正常。 我想添加一個按鈕“關閉按鈕”和“一個檢查用戶是否選擇了項目。如果他沒有選擇任何項目並點擊”編輯按鈕“它將顯示一個MessageBox給用戶選擇一個項目要編輯。我不清楚的是如何通過“選擇項目來做這兩件事”並在關閉面板之前檢查是否有文本框字段中的文本。用戶視圖模型代碼:

public class UserViewModel
{
    private IList<User> _UsersList;
    public UserViewModel()
    {
        _UsersList = new List<User>
    {
        new User { UserId = 1, FirstName = "Raj", LastName = "Beniwal", City = "Delhi", State = "DEL", Country = "INDIA" },
        new User { UserId = 2, FirstName = "Mark", LastName = "henry", City = "New York", State = "NY", Country = "USA" },
        new User { UserId = 3, FirstName = "Mahesh", LastName = "Chand", City = "Philadelphia", State = "PHL", Country = "USA" },
        new User { UserId = 4, FirstName = "Vikash", LastName = "Nanda", City = "Noida", State = "UP", Country = "INDIA" },
        new User { UserId = 5, FirstName = "Harsh", LastName = "Kumar", City = "Ghaziabad", State = "UP", Country = "INDIA" },
        new User { UserId = 6, FirstName = "Reetesh", LastName = "Tomar", City = "Mumbai", State = "MP", Country = "INDIA" },
        new User { UserId = 7, FirstName = "Deven", LastName = "Verma", City = "Palwal", State = "HP", Country = "INDIA" },
        new User { UserId = 8, FirstName = "Ravi", LastName = "Taneja", City = "Delhi", State = "DEL", Country = "INDIA" }           
    };
}

public IList<User> Users
{
    get { return _UsersList; }
    set { _UsersList = value; }           
}

private ICommand mUpdater;

public ICommand UpdateCommand
{
    get
    {
        if (mUpdater == null)
        {
            mUpdater = new Updater();
        }
        return mUpdater;
    }
    set
    {
        mUpdater = value;
    }
}

private class Updater : ICommand
{
    #region ICommand Members

    public bool CanExecute(object parameter)
    {              
        return true;
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
    }

    #endregion
    }
}

用戶視圖Window.Xaml Panel1

<dxdo:LayoutPanel Caption="Panel1" x:Name="Panel1">
    <Grid Margin="0,0,0,20">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <ListView Name="UserGrid" Grid.Row="1" Margin="4,178,12,13" ItemsSource="{Binding Users}">
            <ListView.View>
                <GridView x:Name="grdTest">
                    <GridViewColumn Header="UserId" DisplayMemberBinding="{Binding UserId}" Width="50" />
                    <GridViewColumn Header="First Name" DisplayMemberBinding="{Binding FirstName}" Width="80" />
                    <GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding LastName}" Width="100" />
                    <GridViewColumn Header="City" DisplayMemberBinding="{Binding City}" Width="80" />
                    <GridViewColumn Header="State" DisplayMemberBinding="{Binding State}" Width="80" />
                    <GridViewColumn Header="Country" DisplayMemberBinding="{Binding Country}" Width="100" />
                </GridView>
             </ListView.View>
          </ListView>
       </Grid>
</dxdo:LayoutPanel>
<dxdo:LayoutPanel x:Name="Panel3">
    <Grid>
        <StackPanel>
            <Button Content="Edit" Height="23" HorizontalAlignment="Left" VerticalAlignment="Top" Width="141" Click="Button_Click_1" />
        </StackPanel>
    </Grid>
</dxdo:LayoutPanel>

是Panel2:

<dxdo:LayoutPanel Caption="Panel2" x:Name="Panel2">
    <Grid>
        <StackPanel Margin="0,0,0,10">
            <Grid Margin="0,0,0,20">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <TextBox Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="80,7,0,0" Name="txtUserId" VerticalAlignment="Top" Width="178" Text="{Binding ElementName=UserGrid, Path=SelectedItem.UserId}" />
                <TextBox Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="80,35,0,0" Name="txtFirstName" VerticalAlignment="Top" Width="178" Text="{Binding ElementName=UserGrid, Path=SelectedItem.FirstName}" />
                <TextBox Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="80,62,0,0" Name="txtLastName" VerticalAlignment="Top" Width="178" Text="{Binding ElementName=UserGrid, Path=SelectedItem.LastName}" />
                <Label Content="UserId" Grid.Row="1" HorizontalAlignment="Left" Margin="12,12,0,274" Name="label1" />
                <Label Content="Last Name" Grid.Row="1" Height="28" HorizontalAlignment="Left" Margin="12,60,0,0" Name="label2" VerticalAlignment="Top" />
                <Label Content="First Name" Grid.Row="1" Height="28" HorizontalAlignment="Left" Margin="12,35,0,0" Name="label3" VerticalAlignment="Top" />
                <TextBox Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="80,143,0,0" x:Name="txtCity" VerticalAlignment="Top" Width="178" Text="{Binding SelectedItem.City, ElementName=UserGrid}" />
                <Label Content="Country" Grid.Row="1" Height="28" HorizontalAlignment="Left" Margin="12,141,0,0" x:Name="label2_Copy" VerticalAlignment="Top" />
                <TextBox Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="80,88,0,0" x:Name="txtCountry" VerticalAlignment="Top" Width="178" Text="{Binding SelectedItem.Country, ElementName=UserGrid}" />
                <Label Content="City" Grid.Row="1" Height="28" HorizontalAlignment="Left" Margin="12,86,0,0" x:Name="label2_Copy1" VerticalAlignment="Top" />
                <TextBox Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="80,115,0,0" x:Name="txtSTate" VerticalAlignment="Top" Width="178" Text="{Binding SelectedItem.State, ElementName=UserGrid}" />
                <Label Content="State" Grid.Row="1" Height="28" HorizontalAlignment="Left" Margin="12,113,0,0" x:Name="label2_Copy2" VerticalAlignment="Top" />
            </Grid>
            <Button Content="Update" Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="310,40,0,0" Name="btnUpdate" VerticalAlignment="Top" Width="141" Command="{Binding Path=UpdateCommad}" />
            <Button Content="Close" Grid.Row="1" Height="23" HorizontalAlignment="Right" VerticalAlignment="Top" Width="141" Click="Button_Click_2" />
            <TextBox Width="166" Background="White" Height="33"  HorizontalAlignment="Right" VerticalAlignment="Bottom" Text="{Binding Path=SelectedCustomer.LastName,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" />    
        </StackPanel>
    </Grid>
</dxdo:LayoutPanel>    

(如果我的問題不清楚你請問我。謝謝你)

在ViewModel中創建一個屬性以保存所選用戶:

public User SelectedUser { get; set; }

將ListView的SelectedItem綁定到此屬性:

<ListView Name="UserGrid" ItemsSource="{Binding Users}" SelectedItem="{Binding SelectedUser}">

現在您只需檢查SelectedUser屬性是否為null。

如何通過“選擇項目做這兩件事”

這通常可以使用視圖模型中的SelectedSomething屬性來實現。 此屬性應綁定到SelectedItem的控件。

並在關閉面板之前檢查是否有文本框字段中的任何文本

這稱為“驗證”。 WPF以多種方式支持驗證,其中一種方法是在視圖模型中實現IDataErrorInfo 我推薦這種方式,因為它是.NET的事實標准(它也用於WinForms和ASP .NET)。

一檢查用戶是否選擇了項目。 如果他沒有選擇任何項目並單擊“編輯按鈕”它將向用戶顯示一個MessageBox以選擇要編輯的項目

通常,這通過ICommand實例解決,綁定到按鈕。 如果ICommand.CanExecute返回false,則綁定按鈕將被禁用。

例如,您應檢查CanExecute for command中的驗證錯誤,綁定到Close按鈕,如果有任何錯誤,則返回false。 或者,如果SelectedSomething == null ,則應檢查CanExecute SelectedSomething屬性以獲取命令,綁定到“ Edit按鈕,並返回false

在MVVM中,如果使用RelayCommand / DelegateCommand作為默認的ICommand實現,事情變得更容易。

更新

命令的代碼示例。 查看型號:

public class UserViewModel
{
    public UserViewModel()
    {
        EditCommand = new RelayCommand(EditSelectedUser, () => SelectedUser != null);
    }

    private void EditSelectedUser()
    {
        // some edit code here
    }

    public User SelectedUser { get; set; }
    public ICommand EditCommand { get; private set; }
}

XAML:

<Button Content="Edit" Command="{Binding EditCommand}"/>

暫無
暫無

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

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