1 回答

TA貢獻(xiàn)1951條經(jīng)驗(yàn) 獲得超3個(gè)贊
這里有太多錯(cuò)誤,向您展示正確的代碼比解釋所有內(nèi)容更容易。閱讀我上面鏈接的文章并研究這段代碼。我還沒有完全按照我的方式重新設(shè)計(jì):例如,沒有主視圖模型?,F(xiàn)在這不是 MVVM 的一個(gè)很好的示例,但它說明了您在網(wǎng)格中放置了哪些內(nèi)容、如何編寫模板列以及如何正確更新屬性。
首先,您使用用戶控件的實(shí)例作為同一控件的視圖模型,但它并不是真正的視圖模型,因?yàn)樗鼜牟灰l(fā)任何屬性更改通知。讓我們?yōu)榫W(wǎng)格編寫一個(gè)實(shí)際的項(xiàng)目視圖模型。這不是 UI 控件。它是數(shù)據(jù),將顯示在UI 控件中。它擁有信息,并且當(dāng)信息發(fā)生變化時(shí)它會(huì)收到通知。如果你愿意的話,它也可以有一些邏輯。
public class SliderItem : ObservableObject
{
? ? public SliderItem()
? ? {
? ? }
? ? public SliderItem(int trans, int rot, int scale)
? ? {
? ? ? ? Translation = trans;
? ? ? ? Rotation = rot;
? ? ? ? Scale = scale;
? ? }
? ? public void UpdateValues(int newTrans, int newRot, int newScale)
? ? {
? ? ? ? Translation = newTrans;
? ? ? ? Rotation = newRot;
? ? ? ? Scale = newScale;
? ? }
? ? public void UpdateDescription(string newText)
? ? {
? ? ? ? if(!String.IsNullOrWhiteSpace(newText))
? ? ? ? {
? ? ? ? ? ? Description = newText;
? ? ? ? ? ? OnPropertyChanged(nameof(Description));
? ? ? ? }
? ? }
? ? public String Description { get; private set; }
? ? private int _translation = 0;
? ? public int Translation
? ? {
? ? ? ? get { return _translation; }
? ? ? ? set
? ? ? ? {
? ? ? ? ? ? if (value != _translation)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? _translation = value;
? ? ? ? ? ? ? ? OnPropertyChanged(nameof(Translation));
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? private int _rotation = 0;
? ? public int Rotation
? ? {
? ? ? ? get { return _rotation; }
? ? ? ? set
? ? ? ? {
? ? ? ? ? ? if (value != _rotation)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? _rotation = value;
? ? ? ? ? ? ? ? OnPropertyChanged(nameof(Rotation));
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? private int _scale = 0;
? ? public int Scale
? ? {
? ? ? ? get { return _scale; }
? ? ? ? set
? ? ? ? {
? ? ? ? ? ? if (value != _scale)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? _scale = value;
? ? ? ? ? ? ? ? OnPropertyChanged(nameof(Scale));
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
TripleSlider.xaml
TripleSlider 的 XAML 基本上沒問題;主要問題是它正在尋找以前不存在的視圖模型。但我們還希望滑塊值綁定在Value更改時(shí)更新綁定屬性,而不是在滑塊控件失去焦點(diǎn)時(shí)(這是不明顯的默認(rèn)行為)。因此,添加UpdateSourceTrigger=PropertyChanged到所有三個(gè) Slider.Value 綁定。
? ? Value="{Binding Path=Translation, UpdateSourceTrigger=PropertyChanged}"?
TripleSlider.xaml.cs
就是這個(gè)。這就是該類應(yīng)該的樣子。
public partial class TripleSlider : UserControl
{
? ? public TripleSlider()
? ? {
? ? ? ? InitializeComponent();
? ? }
}
MainWindow.xaml 沒問題。MainWindow.xaml.cs 做了一些更改:
public partial class MainWindow : Window
{
? ? //? Don't use arrays. Use ObservableCollection<WhateverClass> for binding to UI controls,
? ? //? use List<Whatever> for anything else.?
? ? private ObservableCollection<SliderItem> _sliders = new ObservableCollection<SliderItem>();
? ? public MainWindow()
? ? {
? ? ? ? InitializeComponent();
? ? ? ? //? The ObservableCollection will notify the grid when you add or remove items
? ? ? ? //? from the collection. Set this and forget it. Everywhere else, interact with?
? ? ? ? //? _sliders, and let the DataGrid handle its end by itself.?
? ? ? ? //? Also get rid of EnableRowVirtualization="False" from the DataGrid. Let it?
? ? ? ? //? virtualize.?
? ? ? ? myDataGrid.ItemsSource = _sliders;
? ? }
? ? private void BAddControls_Click(object sender, RoutedEventArgs e)
? ? {
? ? ? ? for (int i = 0; i < 49; i++)
? ? ? ? {
? ? ? ? ? ? var newSlider = new SliderItem();
? ? ? ? ? ? newSlider.UpdateDescription(String.Format("{0}: Unkown Value", (i+1)));
? ? ? ? ? ? newSlider.UpdateValues((i+1)*1337, (i+1)*1337, (i+1)*1337);
? ? ? ? ? ? _sliders.Add(newSlider);
? ? ? ? }
? ? ? ? bAddControls.IsEnabled = false;
? ? }
? ? private void BFetchValues_Click(object sender, RoutedEventArgs e)
? ? {
? ? ? ? if (myDataGrid.SelectedItem != null)
? ? ? ? {
? ? ? ? ? ? var selectedSlider = myDataGrid.SelectedItem as SliderItem;
? ? ? ? ? ? MessageBox.Show(String.Format("Translation: {0}\nRotation: {1}\nScale: {2}", selectedSlider.Translation, selectedSlider.Rotation, selectedSlider.Scale), "Information", MessageBoxButton.OK, MessageBoxImage.Information);
? ? ? ? }
? ? }
? ? private void MyDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
? ? {
? ? ? ? bFetchValues.IsEnabled = (myDataGrid.SelectedItem != null) ? true : false;
? ? }
}
- 1 回答
- 0 關(guān)注
- 114 瀏覽
添加回答
舉報(bào)