【问题标题】:Set Content of an Element without a Name-Property设置没有名称属性的元素的内容
【发布时间】:2019-07-04 21:13:14
【问题描述】:

我将 ListBox 与 ObservableCollection 结合使用。内容通过 TemplateSelector(TextBlock 或 Label)设置。文本必须格式化(例如,在代码隐藏中使用运行标签),但我无法访问项目。有没有办法获取元素?

我尝试过使用 OfType,但这仅适用于面板。我搜索了一个儿童属性,但是没有一个用于 ListBoxes 的属性。 UId 和 Name 无法通过绑定设置 Name-Property。 LogicalChildren 的 IEnumerator 不起作用,并且每次添加新元素时都会遍历整个内容,并不是那么理想。这是一个最小的例子。

<Window.Resources>
     <DataTemplate x:Key="TextBlockTemplate">
        <StackPanel>
            <TextBlock />
        </StackPanel>
    </DataTemplate>

     <DataTemplate x:Key="LabelTemplate">
        <StackPanel>
            <Label/>
        </StackPanel>
    </DataTemplate>

    <local:myTemplateSelector x:Key="myTemplateSelector" x:Name="myTemplateSelector" TextBlockTemplate="{StaticResource TextBlockTemplate}" LabelTemplate="{StaticResource LabelTemplate}"/>
</Window.Resources>

<Grid Margin="0">
    <ListBox Name="mylist" Grid.Row="3" 
             ScrollViewer.VerticalScrollBarVisibility="Visible" 
             ScrollViewer.HorizontalScrollBarVisibility="Disabled"  
             ItemsSource="{Binding _listEntries}"                 
             ItemTemplateSelector="{StaticResource myTemplateSelector}"
             >
    </ListBox>
</Grid>

问候和感谢 :)

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    TextBlock 有一个Inlines 属性,它返回构成TextBlock 内容的Inline 元素。

    Label 有一个Content 属性,您可以根据您使用它的方式将其转换为Panel

    TextBox 没有内联元素。

    【讨论】:

    • 您好,感谢您的回答。但我无权访问标签/文本块,因为我无法在没有名称的情况下引用它:)
    • 你想在哪里访问它们?
    • 在 MainWindow.xaml.cs ;) 我找到了一个解决方案并发布了它。
    【解决方案2】:

    现在,我找到了解决方案。我将 TextBlock 和 Label 作为用户控件并设置了 Name-property。在代码隐藏中,我可以访问 DataContext 并且元素可以自行设置。

    <UserControl x:Class="Test.TextBlockControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:TextBlockControl"
             Loaded="UserControl_Loaded">
    <Grid>
        <StackPanel HorizontalAlignment="Stretch"  Margin="0,0,0,0">
            <TextBlock Name="textBlock"/>
        </StackPanel>
    </Grid>
    

    在后面的代码中,我现在可以访问值并设置:

        public partial class TextBlockControl : UserControl
    {
        public List<string> name => DataContext as List<string>;
    
        public TextBlockControl()
        {
            InitializeComponent();
        }
    
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
                foreach (var t in name)
                {
                    var run = new Run(t.Text);
                    if (t.IsHighlighted)
                    {
                        run.Foreground = Brushes.Green;
                    }
                    else
                    {
                        run.Foreground = Brushes.Red;
                    }
                    textBlock.Inlines.Add(run);
                }
            }
        }
    }
    

    在 MainWindow 中,dataTemplate 然后引用 UserControl(root 是命名空间):

    <root:PickControl />
    

    【讨论】:

      猜你喜欢
      • 2010-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-06
      • 1970-01-01
      • 2012-06-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多