C# 在WPF中将属性从CustomControl绑定到页面
我想将CustomControl中的属性绑定到我的页面,然后返回CustomControl,计算页面数量并将其显示在列表中。 我的代码看起来像 海关控制C# 在WPF中将属性从CustomControl绑定到页面,c#,wpf,binding,custom-controls,C#,Wpf,Binding,Custom Controls,我想将CustomControl中的属性绑定到我的页面,然后返回CustomControl,计算页面数量并将其显示在列表中。 我的代码看起来像 海关控制 public partial class CustomControl : UserControl public CustomControl() { InitializeComponent() } public int PageSelected { get { return
public partial class CustomControl : UserControl
public CustomControl()
{
InitializeComponent()
}
public int PageSelected
{
get
{
return (int)GetValue(PageSelectedProperty);
}
set
{
SetValue(PageSelectedProperty, value);
}
}
public static readonly DependencyProperty PageSelectedProperty = DependencyProperty.Register("PageSelected", typeof(int), typeof(CustomControl), new PropertyMetadata(null));
public int RecordsPerPage
{
get
{
return (int)GetValue(RecordsPerPageProperty);
}
set
{
SetValue(RecordsPerPageProperty, value);
}
}
public static readonly DependencyProperty RecordsPerPageProperty = DependencyProperty.Register("RecordsPerPage", typeof(int), typeof(CustomControl), new PropertyMetadata(null));
public IList<int> RecordsPerPageList
{
get
{
return (IList<int>)GetValue(RecordsPerPageListProperty);
}
set
{
SetValue(RecordsPerPageListProperty, value);
}
}
public static readonly DependencyProperty RecordsPerPageListProperty = DependencyProperty.Register("RecordsPerPageList", typeof(List<int>), typeof(CustomControl), new PropertyMetadata(null));
public int RecordsCount
{
get
{
return (int)GetValue(RecordsCountProperty);
}
set
{
SetValue(RecordsCountProperty, value);
CreatePagesList();
}
}
public static readonly DependencyProperty RecordsCountProperty = DependencyProperty.Register("RecordsCount", typeof(int), typeof(CustomControl), new PropertyMetadata(null));
public IList<int> PagesList
{
get
{
return (IList<int>)GetValue(PagesListProperty);
}
set
{
SetValue(PagesListProperty, value);
}
}
public static readonly DependencyProperty PagesListProperty = DependencyProperty.Register("PagesList", typeof(List<int>), typeof(CustomControl), new PropertyMetadata(null));
public int PagesCount
{
get
{
return (int)GetValue(PagesCountProperty);
}
set
{
SetValue(PagesCountProperty, value);
}
}
public static readonly DependencyProperty PagesCountProperty = DependencyProperty.Register("PagesCount", typeof(int), typeof(CustomControl), new PropertyMetadata(null));
public部分类CustomControl:UserControl
公共海关管制()
{
初始化组件()
}
已选择公共int页
{
得到
{
返回(int)GetValue(PageSelectedProperty);
}
设置
{
设置值(PageSelectedProperty,值);
}
}
公共静态只读DependencyProperty PageSelectedProperty=DependencyProperty.Register(“PageSelected”、typeof(int)、typeof(CustomControl)、new PropertyMetadata(null));
公共int记录页面
{
得到
{
返回(int)GetValue(RecordsPerPageProperty);
}
设置
{
SetValue(RecordsPerPageProperty,值);
}
}
public static readonly dependencProperty RecordsPerPageProperty=dependencProperty.Register(“RecordsPerPage”、typeof(int)、typeof(CustomControl)、newpropertyMetadata(null));
公共IList RecordsPerPageList
{
得到
{
返回(IList)GetValue(RecordsPerPageListProperty);
}
设置
{
SetValue(RecordsPerPageListProperty,值);
}
}
公共静态只读DependencyProperty RecordsPerPageListProperty=DependencyProperty.Register(“RecordsPerPageList”、typeof(List)、typeof(CustomControl)、new PropertyMetadata(null));
公共档案室
{
得到
{
返回(int)GetValue(RecordsCountProperty);
}
设置
{
SetValue(RecordsOntProperty,值);
CreatePagesList();
}
}
public static readonly dependencProperty RecordsCountProperty=dependencProperty.Register(“RecordsCount”、typeof(int)、typeof(CustomControl)、new PropertyMetadata(null));
公共IList页面列表
{
得到
{
返回(IList)GetValue(PagesListProperty);
}
设置
{
设置值(页面属性,值);
}
}
public static readonly dependencProperty PagesListProperty=dependencProperty.Register(“PagesList”、typeof(List)、typeof(CustomControl)、new PropertyMetadata(null));
公共网页搜索
{
得到
{
返回(int)GetValue(PageScontProperty);
}
设置
{
设置值(PageScontProperty,值);
}
}
公共静态只读DependencyProperty PageScontProperty=DependencyProperty.Register(“PageScont”、typeof(int)、typeof(CustomControl)、new PropertyMetadata(null));
自定义控件xaml
<UserControl x:Class="Mtrx.CustomControls.CustomControl"
mc:Ignorable="d"
d:DesignHeight="90" Width="200">
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="90" />
</Grid.RowDefinitions>
<ComboBox Width="40" Height="20" Grid.Column="1" Margin="0,5,0,5"
ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=PagesList}"
SelectedItem="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=PageSelected, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
HorizontalContentAlignment="Center"/>
<ComboBox Width="40" Height="20" Grid.Column="9" Margin="0,5,0,5"
ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=RecordsPerPageList, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
SelectedItem="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=RecordsPerPage, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
HorizontalContentAlignment="Center"/>
</Grid>
</UserControl>
第xaml.cs页
public class PageVievModel:AbstractPage
public int RowsCount
{
get
{
return _rowsCount;
}
set
{
if (value != _rowsCount)
{
_rowsCount = value;
RaisePropertyChanged("RowsCount");
}
}
}
private int _rowsCount; //we get it from other place
public int DGRecordsMax
{
get
{
return _dgRecordsMax;
}
set
{
if (value != _dgRecordsMax)
{
_dgRecordsMax = value;
if (value > 0)
{
DataGridRecordsMaxCount = value.ToString();
Settings.Default.Save();
}
RaisePropertyChanged("DGRecordsMax");
}
}
}
private int _dgRecordsMax;
public IList<int> DGRecordsMaxList
{
get
{
return _dGRecordsMaxList;
}
set
{
if (_dGRecordsMaxList != value)
{
_dGRecordsMaxList = value;
RaisePropertyChanged("DGRecordsMaxList");
}
}
}
private IList<int> _dGRecordsMaxList = new List<int>();
public IList<int> PagesList
{
get
{
return _pagesList;
}
set
{
if (_pagesList != value)
{
_pagesList = value;
RaisePropertyChanged("PagesList");
}
}
}
private IList<int> _pagesList = new List<int>();
public int PagesCount
{
get
{
return _pagesCount;
}
set
{
if (value != _pagesCount)
{
_pagesCount = value;
RaisePropertyChanged("PagesCount");
}
}
}
private int _pagesCount;
public IList<int> CurrentPageList
{
get
{
return _currentPageList;
}
set
{
if (_currentPageList != value)
{
_currentPageList = value;
RaisePropertyChanged("CurrentPageList");
}
}
}
private IList<int> _currentPageList;
公共类页面VIEVMODEL:AbstractPage
公共int-ROWSCONT
{
得到
{
返回_rowscont;
}
设置
{
如果(值!=\u行)
{
_rowsCount=值;
RaiseProperty变更(“RowsCount”);
}
}
}
私立国际学校//我们从其他地方得到的
公共整数DGRecordsMax
{
得到
{
返回dgRecordsMax;
}
设置
{
如果(值!=dgRecordsMax)
{
_dgRecordsMax=数值;
如果(值>0)
{
DataGridRecordsMaxCount=value.ToString();
Settings.Default.Save();
}
RaisePropertyChanged(“DGRecordsMax”);
}
}
}
私有int_dgRecordsMax;
公共IList DGRecordsMaxList
{
得到
{
返回dGRecordsMaxList;
}
设置
{
if(dGRecordsMaxList!=值)
{
_dGRecordsMaxList=值;
RaisePropertyChanged(“DGRecordsMaxList”);
}
}
}
私有IList dGRecordsMaxList=新列表();
公共IList页面列表
{
得到
{
返回页面列表;
}
设置
{
如果(_pagesList!=值)
{
_页面列表=值;
RaisePropertyChanged(“页面列表”);
}
}
}
private IList_pagesList=new List();
公共网页搜索
{
得到
{
返回页面搜索;
}
设置
{
如果(值!=\u页面浏览)
{
_PageScont=值;
RaisePropertyChanged(“PageScont”);
}
}
}
私人国际寻呼机;
公共IList当前页面列表
{
得到
{
返回当前页面列表;
}
设置
{
如果(_currentPageList!=值)
{
_currentPageList=值;
RaisePropertyChanged(“当前页面列表”);
}
}
}
私有IList_currentPageList;
页面xaml
<UserControl x:Class="SomeClass"
mc:Ignorable="d"
d:DesignHeight="400" d:DesignWidth="800"
IsEnabled="{Binding AllowInput, Converter={StaticResource AnyToBooleanConverter}}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<DockPanel>
<SomeClass:CustomControl Width="280" Height="190"
RecordsCount="{Binding RowsCount, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
RecordsPerPage="{Binding DGRecordsMax, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay }"
RecordsPerPageList="{Binding DGRecordsMaxList, Mode=TwoWay}"
PagesCount="{Binding PagesCount, Mode=TwoWay}"
PageSelected="{Binding CurrentPage, Mode=TwoWay}"
PagesList="{Binding PagesList, Mode=TwoWay}"
RecordsFrom="{Binding RecordsFrom, Mode=TwoWay}"
RecordsTo="{Binding RecordsTo, Mode=TwoWay}"
DockPanel.Dock="Right"
VerticalAlignment="Bottom"/>
</WrapPanel.Resources>
</WrapPanel>
</Grid>
</UserControl>
当我试着运行我的程序时,会有空的列表,早在我刚开始的时候
using System.Collections.ObjectModel;
....
public ObservableCollection<int> PagesList
{
get
{
return (ObservableCollection<int>)GetValue ( PagesListProperty );
}
set
{
SetValue ( PagesListProperty, value );
}
}
public static readonly DependencyProperty PagesListProperty = DependencyProperty.Register("PagesList", typeof(ObservableCollection<int>), typeof(CustomControl), new PropertyMetadata(null));
<DockPanel>
</WrapPanel>