【问题标题】:Bind header and content dynamically to expander将标题和内容动态绑定到扩展器
【发布时间】:2020-06-16 04:16:49
【问题描述】:

首先我看到 Header 的类型和扩展器的内容,它说类型是对象。我有一个名为 CommonExpanderUserControl 的用户控件,如下所示,

xaml:

<uwpControls:Expander Header="{Binding HeaderContent}" Content="{Binding MainContent}">

</uwpControls:Expander>

在xaml.cs中(DataContext设置为this)

  public static readonly DependencyProperty HeaderContentProperty =
      DependencyProperty.Register("HeaderContent", typeof(object), typeof(CommonExpanderUserControl), new
         PropertyMetadata(null));

        public object HeaderContent
        {
            get { return (object)GetValue(HeaderContentProperty); }
            set { SetValue(HeaderContentProperty, value); }
        }


        public static readonly DependencyProperty MainContentProperty =
    DependencyProperty.Register("MainContent", typeof(ContentControl), typeof(CommonExpanderUserControl), new
       PropertyMetadata(null));

        public ContentControl MainContent
        {
            get { return (ContentControl)GetValue(MainContentProperty); }
            set { SetValue(MainContentProperty, value); }
        }

现在我在外面的某个地方使用这个 UserControl,如下所示,

<UserControl.Resources>
<ContentControl x:Key="Header">
                <Grid x:Name="ExpanderHeaderGrid" HorizontalAlignment="Stretch" Padding="0" Margin="0" 
                      Background="{Binding LisSharedSettings.ChangeHeaderColor,Converter={StaticResource BoolToSolidBrushConverter}}">
                    <TextBlock x:Name="TextBlockLisSharedSettingsTitle" 
                                   x:Uid="/Application.GlobalizationLibrary/Resources/InstrumentSettingsViewLisSettingsTextBlockTitle"
                                   Style="{StaticResource TextBlockStyleSectionHeader}"/>
                </Grid>
            </ContentControl>

<ContentControl x:Key="Body">
Some content here.
</ContentControl>
</UserControl.Resources>
<Grid>
            <local:CommonExpanderUserControl HeaderContent="{StaticResource Header}" MainContent="{StaticResource Body}"/>
</Grid>

像这样绑定内容控件根本行不通。如果我删除 MainContent 绑定并仅绑定 Header,则表示对象引用未设置为对象的实例。请帮忙。

【问题讨论】:

    标签: wpf xaml uwp expander


    【解决方案1】:

    问题出现StaticResource 绑定,我们无法通过StaticResource 控制绑定标头。对于这种情况,更好的方法是绑定 HeaderTemplate 并将数据源发送到 header 属性,如下所示。

    <UserControl.Resources>
        <DataTemplate x:Key="HeaderTemplate">
            <Grid
                x:Name="ExpanderHeaderGrid"
                Margin="0"
                Padding="0"
                HorizontalAlignment="Stretch"
                Background="Red"
                >
                <TextBlock x:Name="TextBlockLisSharedSettingsTitle" Text="{Binding}" />
    
            </Grid>
        </DataTemplate>
    </UserControl.Resources>
    <Grid>
        <uwpControls:Expander Header="hello" HeaderTemplate="{StaticResource HeaderTemplate}" />
    </Grid>
    

    【讨论】:

      猜你喜欢
      • 2012-12-21
      • 1970-01-01
      • 2012-08-09
      • 2011-06-10
      • 2015-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-30
      相关资源
      最近更新 更多