- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个 DataTemplate,其中有一个带有 2 个 RowDefinition 的网格布局。我在第一行有一个 TextBox,在第二行有一个 ComboBox。
我已经在我的 ResourceDictionary 中定义了 DataTemplate。
这是数据模板的代码:
<DataTemplate x:Key="myDataTemplate">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBox Name ="txtChannelDescription" Grid.Row="0" Margin="1,1,1,1"/>
<ComboBox Name="cmbChannelTag" Grid.Row="1" IsReadOnly="True" Margin="1,1,1,1"/>
</Grid>
</DataTemplate>
我在后面的代码中使用这个 DataTemplate 作为:
(数据模板)FindResource(“我的数据模板”)
如何在运行时设置 TextBox.Text 的值和 ComboBox 的 ItemSource?我使用 DataTemplate 作为 DataGridTemplateColumn.Header 的模板。
最佳答案
创建适合您目的的自定义数据类型(在此示例中使用名为 ChannelDescription
和 ChannelTag
的属性),然后将值绑定(bind)到您的 DataTemplate
:
<DataTemplate x:Key="myDataTemplate" DataType="{x:Type NamespacePrefix:YourDataType}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBox Text="{Binding ChannelDescription}" Grid.Row="0" Margin="1,1,1,1"/>
<ComboBox ItemsSource="{Binding ChannelTag}" Grid.Row="1" IsReadOnly="True"
Margin="1,1,1,1"/>
</Grid>
</DataTemplate>
它会像这样使用:
在您的 View 模型中,您将拥有一个集合属性,假设名为 Items
:
public ObservableCollection<YourDataType> Items { get; set; }
(您的属性应该实现 INotifyPropertyChanged
接口(interface),这与此不同)
在您看来,您将拥有一个集合控件,比方说一个 ListBox
:
<ListBox ItemsSource="{Binding Items}" ItemTemplate="{StaticResource myDataTemplate}" />
然后集合控件中的每个项目将具有相同的 DataTemplate
,但值将来自 Items
中类型 YourDataType
的实例> 收藏。
关于c# - 更改 DataTemplate 中存在的 TextBlock 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18120696/
可以通过 TextBlock 文本值设置 TextBlock 的前景属性吗? 例如:文本值是Mike,前景属性是Black,值是Tim,属性值是green等。我用google搜索,但没有找到任何解决方
我有一个显示大量信息的 ListView ,但是当它为空时,我想在其上覆盖一个文本 block ,上面写着“没有要显示的信息”或“bla-bla-bla 添加信息”。 ListView 设置为响应鼠标
我正在阅读 Scott Meyers 的 Effective C++ 3rd。 在第 3 项中: Use const whenever possible. In order to use const
我什么时候使用 的文本属性我什么时候应该把我的文字放在 的内容中? ? vs. Example Text 最佳答案 前者可以绑定(bind),而后者在组合 Run
作为背景,我有一个很长的 ID,它太长而无法在 TextBlock 的给定区域中显示。 ID 的有趣部分是结尾,也就是最右边的部分。 我想要做的是 TextBlock,而不是文本向右溢出并切断最右边的
我正在使用 MVVM 模式,并且我的 ModelView 中有字符串类型属性。 该字符串可能包含以下 HTML 标记: , , , 我需要将 TextBlock 中的某些文本部分设为正常、粗体或斜
我想在屏幕上显示一个数字。如果该数字为 0,我根本不希望它显示。 在常规触发器未能解决我的问题后,我尝试了上述代码
我有一个 TextBlock 和一个 Rectangle,它们都位于一个空的 WPF4 窗口中。 TextBlock 的 Foreground 和 Rectangle 的 Fill 都设置为值为 #8
我有一个 TextBlock其中可能包含很长的文本,所以我想为其添加一个垂直滚动条。我最初的尝试是包装一个 ScrollViewer周围。这行得通,但问题是当我放大时,宽度也被放大了。我尝试像这样禁用
TextBlock 控件中是否可以使用边距文本? 我对 textBlock 控件的风格在这里: --> -->
我的 DataGrid 中的专栏之一包含 Hyperlink在 TextBlock . When a row is selected, the hyperlink shows as blue on b
如果将 TextWrapping 设置为“Wrap”,则 WPF TextBlock 可以包含多行文本。 是否有一种“干净”的方式来获取文本行数?我考虑查看所需的高度并将其除以每条线的估计高度。然而,
有没有办法在控件下方的 TextBlock 中显示错误内容,类似于以下设置工具提示以包含错误文本的方式?
在下面的 XAML 中,我试图包装绑定(bind)到“PortfolioCodes”和“CommentaryText”的 TextBlock,但似乎“Wrapping”不适用于 TextBlock。我
我有一个StackPanel,但以下行: 不包装文字。
我有一个包含大约 15-20 个文本 block 的屏幕,每个文本 block 绑定(bind)到不同的属性,起初所有的文本 block 都是空的,文本更新来自其他客户端。 我想做的是在文本更改时为闪
我有一个名为“findListText”的文本 block 。在这里,我正在更新其中的文本: private void InstantSearch(object sender, KeyEventArg
我试图找出创建样式/触发器以将前景设置为红色的最佳方法,当值 关于WPF Te
我确实有一个 WPF 应用程序实时显示数据(从另一个线程计算)。但是我的 UI 组件(这里是 TextBlock)更新非常缓慢。 我使用传统的数据绑定(bind) PropertyChanged通知。
我想读取一个文件,然后根据某些条件,用不同的颜色标记一些行。 我发现了类似的问题和答案,但它不是使用 MVVM 模式编写的: Selective coloring on dynamic TextBlo
我是一名优秀的程序员,十分优秀!