如何将日期传递给UWP中的UserControl?

时间:2017-12-05 23:19:57

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

我正在尝试将我的页面中的一些数据发送到UWP中的usercontrol。它是我的ExpensesPage上的主/细节控件。我设置了一个DataTemplate,然后在xaml中添加它作为我的ItemTemplate和DetailsTemplate:

<DataTemplate x:Key="ItemTemplate" x:DataType="model:Category">
    <Grid Height="54" Padding="0,8">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <FontIcon Grid.Column="0" FontSize="40" Glyph="{x:Bind SymbolAsChar}" VerticalAlignment="Center" />
        <StackPanel Grid.Column="1" Margin="12,0,0,0" VerticalAlignment="Center">
            <TextBlock Text="{x:Bind CategoryName}" Style="{ThemeResource ListTitleStyle}"/>
            <TextBlock Text="{x:Bind CategorySummary}" Style="{ThemeResource ListSubTitleStyle}"/>
        </StackPanel>
    </Grid>
</DataTemplate>

<DataTemplate x:Key="DetailsTemplate">
    <views:ExpensesDetailControl MasterMenuItem="{Binding}" FilterDate="{Binding FilterDateVariable, Mode=OneWay}"/>
</DataTemplate>


<controls:MasterDetailsView
    Grid.Row="1"
    x:Name="MasterDetailsViewControl"
    ItemsSource="{x:Bind Categories, Mode=OneWay}"
    SelectedItem="{x:Bind Selected, Mode=OneWay}"
    ItemTemplate="{StaticResource ItemTemplate}"
    DetailsTemplate="{StaticResource DetailsTemplate}"
    NoSelectionContentTemplate="{StaticResource NoSelectionContentTemplate}"            
    BorderBrush="Transparent" />

在我的ExpensesPage代码后面我设置如下:

public ObservableCollection<Category> Categories { get; private set; } = new ObservableCollection<Category>();
private DateTime _filterDate;
public DateTime FilterDateVariable
{
    get { return _filterDate; }
    set { Set(ref _filterDate, value); }
}

然后在我的ExpensesDetailControl代码后面我有这段代码:

public Category MasterMenuItem
{
    get { return GetValue(MasterMenuItemProperty) as Category; }
    set { SetValue(MasterMenuItemProperty, value); }
}

public DateTime FilterDate
{
    get { return DateTime.Parse(GetValue(FilterProperty).ToString()); }
    set { SetValue(FilterProperty, value); }
}

public static readonly DependencyProperty MasterMenuItemProperty = DependencyProperty.Register("MasterMenuItem", typeof(Category), typeof(ExpensesDetailControl), new PropertyMetadata(null, OnMasterMenuItemPropertyChanged));

public static readonly DependencyProperty FilterProperty = DependencyProperty.Register("FilterDate", typeof(DateTime), typeof(ExpensesDetailControl), new PropertyMetadata(DateTime.Today, OnFilterPropertyChanged));

private static void OnMasterMenuItemPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    var control = d as ExpensesDetailControl;
    control.ForegroundElement.ChangeView(0, 0, 1);
}

private static void OnFilterPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    var control = d as ExpensesDetailControl;
    //should be able to filter the contents with the date here somehow!

}

MasterMenuItem被触发并按预期工作,但FilterDate没有。

我希望在更改FilterDateVariable时更改属性,但它似乎没有进入ExpensesDetailControl。我做错了什么,如何将我的约会日期发送到我的ExpensesDetailControl?即使在阅读了很多其他线索和信息后,我也不知所措。这对我来说是个新鲜事。感谢

0 个答案:

没有答案