将CustomControl的属性绑定到WPF中的页面

问题描述 投票:0回答:1

我想将属性从CustomControl绑定到我的页面,然后返回到CustomControl并计算页面数量并将其显示在列表中。我的代码如下。

CustomControl

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));

自定义控件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>

Page 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;

Page 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>

[当我尝试运行程序时,列表为空,之前我只是在Page中保留了更多属性,它运行良好。

我将很乐意提供帮助。我很难理解如何使这两种方式都具有可绑定属性。

c# wpf binding custom-controls
1个回答
0
投票

我试图使用您的代码来举例说明。

对我来说,如果我将IList和List都更改为ObservableCollection,例如,

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));

请注意,我同时更改了属性定义和DependencyProperty定义。

您的代码有点混乱,例如,您有一个用/ WrapPanel关闭的DockPanel标记,显然它不会编译。

<DockPanel>
</WrapPanel>
© www.soinside.com 2019 - 2024. All rights reserved.