I my scenario, I need to update the Y Coordinate value only if the certain criteria of that value is met. For example, if the value is ‘Not available’ or “0’ or “15”. Then, by clicking “Update” button that value should be changed to “Updated to” and red box to the left of the string can be changed to any other color. I was wondering to find out what will be the right way to do that based on the code below. Any advice is highly appreciated! Thank you!
XAML:
<UserControl.开发者_JAVA百科DataContext>
<local:MainPage_ViewModel/>
</UserControl.DataContext>
<StackPanel Orientation="Horizontal">
<data:DataGrid ItemsSource="{Binding Coordinates}" AutoGenerateColumns="False" Margin="10">
<data:DataGrid.Columns>
<data:DataGridTextColumn Header="X Position" Width="100" Binding="{Binding X}"/>
<data:DataGridTextColumn Header="Y Position" Width="100" Binding="{Binding Y}"/>
<data:DataGridTemplateColumn Header="Delete Item" Width="100">
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Delete" Tag="{Binding}" Click="Button_Click"/>
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>
</data:DataGrid.Columns>
</data:DataGrid>
<StackPanel VerticalAlignment="Top" Margin="0,40,0,0">
<TextBlock x:Name="Click_to_update_value" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top"/>
<Button x:Name="Update_value" Content="Update Value" Margin="0,10,0,0" Click="Button_Update"/>
</StackPanel>
</StackPanel>
Code behind:
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
this.viewModel = this.DataContext as MainPage_ViewModel;
}
private MainPage_ViewModel viewModel;
private void Button_Update(object sender, RoutedEventArgs e)
{
}
}
View Model:
public class MainPage_ViewModel : INotifyPropertyChanged
{
public MainPage_ViewModel()
{
coordinates.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 1, Y = 2 }));
coordinates.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 2, Y = 4 }));
coordinates.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 3, Y = 6 }));
coordinates.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 4, Y = 8 }));
coordinates.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 5, Y = 10 }));
coordinates.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 6, Y = 12 }));
}
public ObservableCollection<Coordinate_DataViewModel> Coordinates
{
get { return coordinates; }
set
{
if (coordinates != value)
{
coordinates = value;
OnPropertyChanged("Coordinates");
}
}
}
private ObservableCollection<Coordinate_DataViewModel> coordinates = new ObservableCollection<Coordinate_DataViewModel>();
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public void DeleteCoordinate(Coordinate_DataViewModel dvmToDelete)
{
coordinates.Remove(dvmToDelete);
}
public void UpdateCoordinate(Coordinate_DataViewModel dvmToDelete)
{
}
}
//Model
public class Coordinate_Model
{
public double X;
public double Y;
}
//DataViewModel
public class Coordinate_DataViewModel
{
public Coordinate_DataViewModel(Coordinate_Model model)
{
this.underlyingModel = model;
}
private Coordinate_Model underlyingModel;
public double X
{
get { return underlyingModel.X; }
set { underlyingModel.X = value; }
}
public double Y
{
get { return underlyingModel.Y; }
set { underlyingModel.Y = value; }
} public string XYCoordinate
{
get { return "(" + X + "," + Y + ")"; }
}
}
You can update the value(s) in the data grid by updating the observable collection. One thing you might want to do is handle events in the viewmodel instead of the code behind. That might make updating a value in the collection that's in the viewmodel, make more sense.
精彩评论