如何将值传递给UserControl(uwp)

时间:2017-07-19 01:42:46

标签: c# user-controls uwp uwp-xaml

如何将值传递给UserControl(uwp)

<Pivot Title="Welcome" Name="View_P" SelectionChanged="Pivot_SelectionChanged" >
    <PivotItem Header="Car" Name="v_1">
        <local:View_Control />
            </PivotItem>

            <PivotItem Header="electronics">
        <local:View_Control/>
    </PivotItem>
</Pivot>

参见PivotItem0和PivotItem0

<local:View_Control/>

下一个代码c#

private void Pivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if( View_P.SelectedIndex == 0)
    {
        View_Control.numbers=1
    }
    else if (View_P.SelectedIndex == 0)
    {
        View_Control.numbers = 2
    }
}

下一个代码UserControl名称页面(View_Control)

public  int numbers;
public View_Control()
{
    ServiceErkaanClient dv = new ServiceErkaanClient();
    this.InitializeComponent();
    MyGrid1.ItemsSource = dv.SP_All_SellAsync(numbers).Result;
}

必需 如果单击Pivot SelectedIndex == 0将参数发送到UserControl(View_Control)1并刷新

1 个答案:

答案 0 :(得分:0)

首先,你应该将名称设置为View_Control,你可以看到下面的代码。

<Pivot Title="Welcome" Name="View_P" 
       SelectionChanged="Pivot_SelectionChanged" >
    <PivotItem Header="Car" Name="v_1">
         <local:View_Control Name="v1"/>
    </PivotItem>

    <PivotItem Header="electronics">
        <local:View_Control Name="v2"/>
    </PivotItem>
 </Pivot>

第二个是将属性更改为财产,您可以看到以下代码。

    private int _numbers;
    public int Numbers
    {
        set
        {
            _numbers=value;

            //when the numbers be changed

            ServiceErkaanClient dv = new ServiceErkaanClient();
            MyGrid1.ItemsSource = dv.SP_All_SellAsync(numbers).Result;
        }
        get
        {
            return _numbers;
        }
    }
    public View_Control()
    {
        ServiceErkaanClient dv = new ServiceErkaanClient();
        this.InitializeComponent();
        MyGrid1.ItemsSource = dv.SP_All_SellAsync(Numbers).Result;
    }

当您将数字设置为1时,它将调用SP_All_SellAsync。

如果您想使用依赖属性,可以尝试以下代码

  public static readonly DependencyProperty NumbersProperty = DependencyProperty.Register(
        "Numbers", typeof(int), typeof(View_Control), new PropertyMetadata(default(int), (s, e) =>
        {
            View_Control view = (View_Control) s;
            int n = (int) e.NewValue;

            ServiceErkaanClient dv = new ServiceErkaanClient();
            view.MyGrid1.ItemsSource = dv.SP_All_SellAsync(n).Result;
        }));

    public int Numbers
    {
        get { return (int) GetValue(NumbersProperty); }
        set { SetValue(NumbersProperty, value); }
    }

    public View_Control()
    {
        ServiceErkaanClient dv = new ServiceErkaanClient();
        this.InitializeComponent();
        MyGrid1.ItemsSource = dv.SP_All_SellAsync(Numbers).Result;
    }

如果您有任何问题,欢迎与我联系。