Мой текстовый блок не обновляется, обновляйте значение из моей модели. Если я обновляю текстовый блок в ViewModel, он работает, поэтому мои привязки кажутся правильными. Я считаю, что проблема заключается в том, как я обновляю ее в модели, но я не уверен, почему моя наблюдаемая коллекция обновляется только потому, что я передаю значения туда и обратно, не уверенный, что это хорошая стратегия MVVM.
XAML-часть:
<Grid>
<TextBox x:Name="NewLabelBx" HorizontalAlignment="Left" Height="23" Margin="54,449,0,0" TextWrapping="Wrap" Text="{Binding NewLabel,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="314"/>
<Button x:Name="NewLabelBtn" Content="Add Label" HorizontalAlignment="Left" Margin="293,490,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="0.518,-0.709" Command="{Binding Path=NewLabelBtn}" />
<TextBlock x:Name="FilesProcessedBlck" HorizontalAlignment="Left" Margin="54,507,0,0" TextWrapping="Wrap" Text="{Binding FilesProcessedBlck, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" RenderTransformOrigin="-0.7,0.562" Width="65"/>
</Grid>
Часть ViewModel:
public class P4LabelBatteryViewModel : BindableBase
{
private P4LabelBatteryModel p4LabelBatteryModel = new P4LabelBatteryModel();
public P4LabelBatteryViewModel()
{
P4LabelBatteryModel p4LabelBatteryModel = new P4LabelBatteryModel();
this.GetBatteryBtn = new DelegateCommand(chooseFile, canChooseFile);
this.NewLabelBtn = new DelegateCommand(chooseNewLabel, canNewLabel).ObservesProperty(() => NewLabel);
this.FilesProcessedBlck = 2; //this works.
}
//other code here
private void chooseNewLabel()
{
if (ScriptCollection.Count > 0)
{
ScriptCollection = P4LabelBatteryModel.TagsFilesModel(NewLabel, ScriptCollection);
}
}
private int _filesProcessedBlck;
public int FilesProcessedBlck
{
get
{
return _filesProcessedBlck;
}
set
{
SetProperty(ref _filesProcessedBlck, value);
}
}
private ObservableCollection<ScriptModel> _scriptCollection = new ObservableCollection<ScriptModel>();
public ObservableCollection<ScriptModel> ScriptCollection
{
get
{
return _scriptCollection;
}
set
{
SetProperty(ref _scriptCollection, value);
}
}
}
Часть модели:
class P4LabelBatteryModel
{
public static ObservableCollection<ScriptModel> TagsFilesModel(string NewLabel, ObservableCollection<ScriptModel> observableCollection)
{
string newLabel = NewLabel;
var scriptsToTagColl = observableCollection;
string[] files = null;
var _p4LabelBatteryViewModel = new P4LabelBatteryViewModel();
_p4LabelBatteryViewModel.FilesProcessedBlck++; //xaml is never updated with this value.
//This will generate an IPC when returned
ObservableCollection<ScriptModel> newCollection = new ObservableCollection<ScriptModel>();
//code here that modifies newCollection xaml updates when this returns, _p4LabelBatteryViewModel.FilesProcessedBlck++; does not.
return newCollection;
}
}
Когда я запускаю отладчик, я вижу, что P4LabelBatteryViewModel.FilesProcessedBlck изменяется, но XAML не обновляется.
.DataContext
. Похоже, вы не назначили его правильно, поэтому ваш TextBlock указывает на один экземплярP4LabelBatteryViewModel
, в то время как вы создаете второй экземпляр в своем кодеObservableCollection
. - person Rachel   schedule 26.10.2016