我的页面上有一个按钮,当我点击它时会弹出另一个小窗口让你进入一个国家。然后,它将在我的主屏幕上显示为标签。
这是保存按钮。
<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
的新实例,其中有一个<String>Country
属性,但也有{{1}名为ICollection<string>
的属性。
Places
当我右键单击主屏幕上的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
。
如果我在屏幕上创建3个标题(因此它会创建3个ICollection<string>Places
的3个不同实例),当我点击AddCountryClass
时,我无法找到Add place
的实例我点击了。
我的ViewModel中的其他有用代码:
Country
这是我创建国家/地区实例的地方,当我进入我的国家/地区
public void AddCountryCollection()
{
NewCountryClass newCountryClass = new NewCountyClass(newCountry,"");
Collections.Add(newCountryClass);
}
这是国家通过的地方**
public ObservableCollection<NewCountryClass> Collections
{
get { return collections; }
set { collections = value; OnPropertyChanged(() => Collections); }
}
这是我试图去做但却没有取得任何成就
摘要
如果我创建3个public void AddPlaces()
{
NewCountryClass newCountryClass;
string Item = SelectedCountry.Country;
Collections.Select(w => w.Country);
}
使用3个不同的类Countrys
实例,当我想在其中一个国家的NewContryClass
中添加内容时,如何找到它?
答案 0 :(得分:0)
声称我不知道非查看内容属于虚拟机,但这就是我这样做的方式而且我很好用
<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); }
}
就是这样。
您还可以查看提供一些this Tut
的Samplecode