【问题标题】:Binding Text to Textbox in HubSection将文本绑定到 HubSection 中的文本框
【发布时间】:2016-10-20 18:44:42
【问题描述】:

这个问题类似于Windows Phone 8.1 Toggling the visibility of a TextBlock in a DataTemplate

还有无数其他想法,但这些想法都没有奏效。将 Textblock 添加到集线器中的数据模板后,永远不会触发 Loaded 事件。视觉树搜索未找到我的 TextBlock。

我尝试过这样的基本绑定:

            <HubSection Background="{StaticResource HubSectionBackgroundBrush}"
                        MaxWidth="{x:Bind DesiredHubSectionWidth, Mode=OneWay}"
                        Header="You have selected:" Padding="60" 
                        >
                <DataTemplate x:DataType="local:Scenario4">
                    <TextBlock TextWrapping="Wrap" Style="{StaticResource BasicTextStyle}" HorizontalAlignment="Left" Text="{Binding Item}"/>
                </DataTemplate> 
            </HubSection>

public string Item { get; set; }
Item = makeText.Text;

但这不起作用(集线器上的文本始终为空)。通过查看以前的帖子和代码,我使用依赖属性提出了这个 xaml 代码:

             <HubSection Background="{StaticResource HubSectionBackgroundBrush}"
                        MaxWidth="{x:Bind DesiredHubSectionWidth, Mode=OneWay}"
                        Header="You have selected:" Padding="60"
                        >
                <DataTemplate x:DataType="local:Scenario4">
                    <TextBlock TextWrapping="Wrap" Style="{StaticResource BasicTextStyle}" HorizontalAlignment="Left" Text="{x:Bind DesiredSelectionText, Mode=OneWay}"/>
                </DataTemplate> 
            </HubSection>

在c#中使用这个

    private static DependencyProperty s_desiredHubSectionWidthProperty
        = DependencyProperty.Register("DesiredHubSectionWidth", typeof(double), typeof(Scenario4), new PropertyMetadata(560.0));

    private static DependencyProperty selectionText = DependencyProperty.Register("SelectionText", typeof(string), typeof(Scenario4), new PropertyMetadata("Nothing"));

    public static DependencyProperty DesiredHubSectionWidthProperty
    {
        get { return s_desiredHubSectionWidthProperty; }
    }

    public static DependencyProperty DesiredSelectionTextProperty
    {
        get { return selectionText; }
    }

    public string DesiredSelectionText
    {
        get { return (string)GetValue(selectionText); }
        set { SetValue(selectionText, value); }
    }

    public double DesiredHubSectionWidth
    {
        get { return (double)GetValue(s_desiredHubSectionWidthProperty); }
        set { SetValue(s_desiredHubSectionWidthProperty, value); }
    }

我用

设置文本
DesiredSelectionText = makeText.Text;

宽度绑定完美,但文本没有更新。在运行时更改 Hub/DataTemplate 文本的正确方法是什么?既然 Hub 连“Nothing”都没有打印出来,那肯定是出了什么问题。

作为最后的手段,我想我只会构建自己的数据模板并在运行时分配它,但我能找到的唯一代码已被弃用(使用 FrameworkElementFactory)。

【问题讨论】:

    标签: c# xaml data-binding win-universal-app


    【解决方案1】:

    我试图在 OnNavigatedTo 方法中分配给文本块,该方法在文本块的 Loaded 方法之前调用。这就是我的程序将文本块显示为 null 的原因。

    原来我发布的链接是解决方案,就我而言,文本块是在页面导航到之后加载的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-15
      • 2022-11-15
      • 2012-05-15
      • 2016-02-12
      • 2017-04-17
      • 1970-01-01
      • 2020-10-06
      • 2011-07-17
      相关资源
      最近更新 更多