我做了Page MainPage
和UserControl Pager
。两者都有其ViewModel。在Pager
中,存在三个依赖项属性Rows
,Columns
和Source
。我想将这些属性从Pager
的View传递给Pager
的ViewModel。我在View的代码后面进行了尝试。但这是行不通的...永远不会在调试时调用set
中的PagerViewModel
属性。请帮我...
这是详细机制:MainPageViewModel
↓通过绑定(bind)传递值MainPage
↓使用MainPagerViewModel
中的值设置属性Pager
(后面的代码)
↓将属性绑定(bind)到PagerViewModel PagerViewModel
↓通过绑定(bind)传递值Pager
(XAML)
这是来源
[MainPageViewModel.cs]
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using Client.Model;
namespace Client.ViewModel
{
public class MainPageViewModel : ViewModelBase
{
...
public ObservableCollection<IPagableEntry> PagerTableCategoriesItems { get { return TableCategoryRepository.Instance.TableCategories; } }
public int PagerTableCategoriesRows { get { return 1; } }
public int PagerTableCategoriesColumns { get { return 3; } }
...
}
}
[MainPage.xaml]
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:view="clr-namespace:Client.View"
xmlns:viewModel="clr-namespace:Client.ViewModel"
xmlns:resStr="clr-namespace:Client.CommonResources.String"
x:Class="Client.View.MainPage"
Style="{StaticResource common}">
<Page.DataContext>
<viewModel:MainPageViewModel />
</Page.DataContext>
...
<view:Pager x:Name="pagerTableCategories"
Source="{Binding Path=PagerTableCategoriesItems}"
Rows="{Binding Path=PagerTableCategoriesRows}"
Columns="{Binding Path=PagerTableCategoriesColumns}">
</view:Pager>
...
</Page>
[Pager.xaml.cs]
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Input;
using Client.Model;
using Client.ViewModel;
namespace Client.View
{
public partial class Pager
{
public static readonly DependencyProperty SourceProperty = DependencyProperty.Register("Source", typeof(ObservableCollection<IPagableEntry>), typeof(Pager), new PropertyMetadata(null, OnSourceChanged));
public static readonly DependencyProperty RowsProperty = DependencyProperty.Register("Rows", typeof(int), typeof(Pager), new PropertyMetadata(1, OnRowsChanged));
public static readonly DependencyProperty ColumnsProperty = DependencyProperty.Register("Columns", typeof(int), typeof(Pager), new PropertyMetadata(1, OnColumnsChanged));
public static readonly DependencyProperty SelectedEntryProperty = DependencyProperty.Register("SelectedEntry", typeof(object), typeof(Pager), new PropertyMetadata(null, OnSelectedEntryChanged));
public int Rows
{
get { return (int)GetValue(RowsProperty); }
set { SetValue(RowsProperty, value); }
}
public int Columns
{
get { return (int)GetValue(ColumnsProperty); }
set { SetValue(ColumnsProperty, value); }
}
public object SelectedEntry
{
get { return GetValue(SelectedEntryProperty); }
set { SetValue(SelectedEntryProperty, value); }
}
public ObservableCollection<IPagableEntry> Source
{
get { return (ObservableCollection<IPagableEntry>)GetValue(SourceProperty); }
set { SetValue(SourceProperty, value); }
}
public Pager()
{
InitializeComponent();
// I want to bind the three custom properties(Rows, Columns, Source) to PagerViewModel's Rows, Columns, Collection
Binding bindingRows = new Binding("Rows");
bindingRows.Mode = BindingMode.TwoWay;
bindingRows.Source = gridPager.DataContext;
gridPager.SetBinding(RowsProperty, bindingRows);
Binding bindingColumns = new Binding("Columns");
bindingColumns.Mode = BindingMode.TwoWay;
bindingColumns.Source = gridPager.DataContext;
gridPager.SetBinding(ColumnsProperty, bindingColumns);
Binding bindingSource = new Binding("Collection");
bindingSource.Mode = BindingMode.TwoWay;
bindingSource.Source = gridPager.DataContext;
gridPager.SetBinding(SourceProperty, bindingSource);
}
private void ListBoxEntriesOnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
SelectedEntry = (sender as ListBox).SelectedItem;
}
private static void OnSelectedEntryChanged(DependencyObject pager, DependencyPropertyChangedEventArgs e)
{
(pager as Pager).SelectedEntry = e.NewValue;
}
private static void OnSourceChanged(DependencyObject pager, DependencyPropertyChangedEventArgs e)
{
(pager as Pager).Source = (ObservableCollection<IPagableEntry>)e.NewValue;
}
private static void OnRowsChanged(DependencyObject pager, DependencyPropertyChangedEventArgs e)
{
(pager as Pager).Rows = (int)e.NewValue;
}
private static void OnColumnsChanged(DependencyObject pager, DependencyPropertyChangedEventArgs e)
{
(pager as Pager).Columns = (int)e.NewValue;
}
}
}
[Pager.xaml]
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:view="clr-namespace:Client.View"
xmlns:viewModel="clr-namespace:Client.ViewModel"
xmlns:resStr="clr-namespace:Client.CommonResources.String"
x:Class="Client.View.Pager">
<Grid x:Name="gridPager">
<Grid.DataContext>
<viewModel:PagerViewModel />
</Grid.DataContext>
...
<ListBox x:Name="listBoxEntries"
ItemsSource="{Binding Path=Collection}"
BorderThickness="0"
Margin="0"
Style="{StaticResource common}"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
ItemTemplate="{StaticResource templateTableCategory}"
SelectedItem="{Binding Path=SelectedEntry, Mode=TwoWay}"
SelectionChanged="ListBoxEntriesOnSelectionChanged">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Rows="{Binding Path=Rows}"
Columns="{Binding Path=Columns}"
IsItemsHost="True"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
...
</Grid>
</UserControl>
[PagerViewModel.cs]
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Data;
using System.Windows.Media;
using Client.Model;
namespace Client.ViewModel
{
public class PagerViewModel : ViewModelBase
{
...
ListCollectionView _listCollectionView;
ObservableCollection<IPagableEntry> _collection;
int _rows;
int _columns;
public int Rows
{
get { return _rows; }
set
{
_rows = value;
OnPropertyChanged();
}
}
public int Columns
{
get { return _columns; }
set
{
_columns = value;
OnPropertyChanged();
}
}
public ListCollectionView ListCollectionView
{
get { return _listCollectionView; }
set
{
_listCollectionView = value;
OnPropertyChanged();
}
}
public ObservableCollection<IPagableEntry> Collection
{
get
{
return _collection;
}
set
{
_collection = value;
OnPropertyChanged();
}
}
...
}
}
最佳答案
您发布的代码有两个明显的问题:
OnXXXChanged()
处理程序不执行任何操作。他们正在响应依次更改的属性设置。 IE。他们只是重申要通知他们的属性设置,而不是在其他对象中设置属性值。 SetBinding()
调用背后的代码的目标是gridPager
对象,它只是一个Grid
。它没有要设置的Rows
,Columns
或Source
属性。 假设一会儿我们将使用绑定(bind)来完成此操作,那么您将遇到第三个问题:
Pager.Rows
已经是在 MainPage.xaml 中建立的绑定(bind)的目标,而PagerTableCategoriesRows
作为源。它也不能成为与任何其他对象的绑定(bind)的目标(最重要的是,不是来自其自身的循环绑定(bind),这是如果使用Pager.RowsProperty
依赖项属性(如代码所尝试的那样)才有意义的唯一来源)。 我尚不清楚这样做的智慧。看来
Pager
元素可以直接直接绑定(bind)到Pager
属性本身,从而从原始 View 模型继承值,而不是维护第二个完全独立但打算相同的 View 模型。但是假设有一个我完全不理解的很好的理由,您打算在这里使用两个不同的 View 模型并希望使其保持同步,在我看来,您应该能够通过更改您的 View 来使其工作
OnXXXChanged()
处理程序,以便它们直接设置 View 模型值。例如。:public partial class Pager
{
public static readonly DependencyProperty SourceProperty = DependencyProperty.Register("Source", typeof(ObservableCollection<IPagableEntry>), typeof(Pager), new PropertyMetadata(null, OnSourceChanged));
public static readonly DependencyProperty RowsProperty = DependencyProperty.Register("Rows", typeof(int), typeof(Pager), new PropertyMetadata(1, OnRowsChanged));
public static readonly DependencyProperty ColumnsProperty = DependencyProperty.Register("Columns", typeof(int), typeof(Pager), new PropertyMetadata(1, OnColumnsChanged));
public static readonly DependencyProperty SelectedEntryProperty = DependencyProperty.Register("SelectedEntry", typeof(object), typeof(Pager));
public int Rows
{
get { return (int)GetValue(RowsProperty); }
set { SetValue(RowsProperty, value); }
}
public int Columns
{
get { return (int)GetValue(ColumnsProperty); }
set { SetValue(ColumnsProperty, value); }
}
public object SelectedEntry
{
get { return GetValue(SelectedEntryProperty); }
set { SetValue(SelectedEntryProperty, value); }
}
public ObservableCollection<IPagableEntry> Source
{
get { return (ObservableCollection<IPagableEntry>)GetValue(SourceProperty); }
set { SetValue(SourceProperty, value); }
}
public Pager()
{
InitializeComponent();
}
private void ListBoxEntriesOnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
SelectedEntry = (sender as ListBox).SelectedItem;
}
private static void OnSourceChanged(DependencyObject pager, DependencyPropertyChangedEventArgs e)
{
((PagerViewModel)(pager as Pager).gridPager.DataContext).Collection =
(ObservableCollection<IPagableEntry>)e.NewValue;
}
private static void OnRowsChanged(DependencyObject pager, DependencyPropertyChangedEventArgs e)
{
((PagerViewModel)(pager as Pager).gridPager.DataContext).Rows =
(int)e.NewValue;
}
private static void OnColumnsChanged(DependencyObject pager, DependencyPropertyChangedEventArgs e)
{
((PagerViewModel)(pager as Pager).gridPager.DataContext).Columns =
(int)e.NewValue;
}
}
顺便说一句:我不建议您在上面使用
as
。主要原因是,如果由于某种原因强制转换失败,那么第一个可见的症状就是不是很有帮助的NullReferenceException
,而不是InvalidCastException
。我建议仅在预计某些时候转换会失败的情况下才使用
as
。当然,在这种情况下,您也将始终检查null
结果并进行适当处理。如果您打算强制转换将始终成功,请使用强制转换运算符。
关于c# - 如何在后面的代码上将View的自定义属性绑定(bind)到ViewModel?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33198852/