簡體   English   中英

檢索動態創建的類的實例

[英]Retrieving instance of class that was created dynamically

我的頁面上有一個按鈕,所以當我單擊它時,會彈出另一個小窗口,讓您輸入國家/地區。 然后,它將在我的主屏幕上顯示為標簽。

這是保存按鈕。

<Button x:Name="btnSave" Content="Save" HorizontalAlignment="Left" Margin="31,82,0,0" VerticalAlignment="Top" Width="75" Click="btnSave_Click"/>

然后我有代碼。

ICommand _AddCountry;
ViewModel _viewmodel;
public NewCountry(ICommand AddCountry, ViewModel viewModel)
{
    InitializeComponent();
    _Addcountry = AddCountry;
    _viewmodel = viewModel;
}

private void btnSave_Click(object sender, RoutedEventArgs e)
{
    string countryName = txtCountry.Text;

    _viewmodel.RenameCountry(countryName);
    _Addcountry.Execute(null);
    this.Close();
}

該標簽已綁定到“標題”,因此“ Country將顯示為該標簽。

<Label Content="{Binding Country}" Width="500" HorizontalAlignment="Left" >

現在這是我的問題,當我在運行時創建該Country時,它會創建NewCountryClass的新實例,該實例在NewCountryClass內具有<String>Country屬性,還具有一個名為PlacesICollection<string>屬性。

public class NewCountryClass  : INotifyPropertyChanged

private string _country;

public string Country
{
   get { return _country; }
   set
   {
      if (_country.Equals(value))
         return;

      _country = value;
      RaisePropertyChanged(() => Country);
   }
}

private ICollection<string> _places;
public ICollection<string> Places
{
   get
   {
      if (_places == null)
         _places = new ObservableCollection<string>();
      return _places;
   }
   set
   {
      if (value == _places)
         return;

      _places = value;
      RaisePropertyChanged(() => Places);
   }
}

當我右擊對Countrys標簽我的主屏幕上,點擊Add Place上下拉,我想那個地方,以獲得加入到Country該類的實例,里面ICollection<string>Places

當我在屏幕上創建3個標題(因此它創建了AddCountryClass 3個不同實例)時,問題就出現了,當我單擊“ Add place找不到所單擊的Country的實例。

我的ViewModel中其他有用的代碼:

public void AddCountryCollection()
{
    NewCountryClass newCountryClass = new NewCountyClass(newCountry,"");
    Collections.Add(newCountryClass);
}

輸入我的國家/地區后,在此創建國家/地區的實例

public ObservableCollection<NewCountryClass> Collections
{
    get { return collections; }
    set { collections = value; OnPropertyChanged(() => Collections); }
}

這是該國家/地區通過的地方**

public void AddPlaces()
{
    NewCountryClass newCountryClass;

    string Item = SelectedCountry.Country;
    Collections.Select(w => w.Country);

}

這是我嘗試的嘗試,但沒有取得任何成就

摘要

如果我創建3個Countrys其中使用類3種不同情況下NewContryClass ,當我想補充一點到ICollection<string>Places該國之一,如何找到它?

免責聲明我知道非視圖內容屬於VM,但這就是我做的方式,我對此很滿意

主電源

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <!--Here is how you could select your Country-->

    <Label Content="Country" Grid.Column="0" Grid.Row="0" />
    <ComboBox Grid.Column="1" Grid.Row="0"
                      DisplayMemberPath="Country" Margin="0,0,30,5"
                      ItemsSource="{Binding CountryCollection}"
                      SelectedItem="{Binding SelectedCountryItem,UpdateSourceTrigger=PropertyChanged}"/>

    <Button Content="+" Grid.Column="1" Grid.Row="0"  Height="24" HorizontalAlignment="Right"  Width="24" Margin="0,0,0,5"
                    Command="{Binding NewCountryCommand}"/>

    <!--Here is how you could show your selected Country-->
    <Label Content="Selected" Grid.Column="0" Grid.Row="1" />
    <Grid Grid.Column="1" Grid.Row="1" DataContext="{Binding SelectedCountryItem}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Label Content="{Binding Country}"  
               Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2"/>

    </Grid>

</Grid>

主虛擬機

    private RelayCommand _NewCountryCommand; // you can find the RelayCommand Class on the web
    public ICommand NewCountryCommand
    {
        get { return _NewCountryCommand ?? (_NewCountryCommand= new RelayCommand(param => this.OnNewCountry())); }
    }
    public void OnNewCountry()
    {
         var window = new Window(); // or whatever View you use
         var vm = new NewCountryClass();

         window.DataContext= vm;
         window.ShowDialog();

         if(vm.Result)// Result will be set to true 
                      // if the user Click the Save Button in your CountryV
         {
              // here will be your logic if the user want to save his stuff
              // in my case
              CountryCollection.Add(vm)
              SelectedCountryItem = vm;
         }
    }


public ObservableCollection<NewCountryClass> CountryCollection
{
    get { return collections; }
    set { collections = value; 
          OnPropertyChanged(() => CountryCollection); }
}

public NewCountryClass SelectedCountryItem
{
    get { return selectedCountryItem; //}
    set { selectedCountryItem = value; 
          OnPropertyChanged(() => SelectedCountryItem); }
}

就是這樣。

您也可以看看這個提供了一些Samplecode的 Tut

暫無
暫無

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

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