C# 如何自定义列表框中的占位符文本,使其在UWP中具有特定颜色
我正在使用C# 如何自定义列表框中的占位符文本,使其在UWP中具有特定颜色,c#,xaml,uwp,C#,Xaml,Uwp,我正在使用文本框的占位符文本属性处理列表框。这是我的列表框的代码。我正在使用弹出窗口来实现内容 <Popup x:Name="ReasonCodePopUp" x:FieldModifier="Public" IsOpen="{Binding ShowFuelChangeReasonPopUp, Mode=TwoWay}" Height="420" Width="1100" Grid.Row="0" Grid.RowSpan=
文本框
的占位符文本
属性处理列表框
。这是我的列表框的代码。我正在使用弹出窗口
来实现内容
<Popup x:Name="ReasonCodePopUp" x:FieldModifier="Public" IsOpen="{Binding ShowFuelChangeReasonPopUp, Mode=TwoWay}"
Height="420" Width="1100" Grid.Row="0" Grid.RowSpan="2" IsLightDismissEnabled="True">
<Grid Height="397" Width="1100" >
<TextBlock Visibility="Collapsed" Name="txtFuelReason" Text="{x:Bind ViewModel.FuelPlanInfo.ExtraFuelReason,Mode=TwoWay}"/>
<ListBox Margin="20" Name="lstFuelReason" SelectionChanged="lstFuelReason_SelectionChanged"
Background="#414042" Height="420" Width="1100" >
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Margin" Value="0"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="Height" Value="40"/>
<Setter Property="Padding" Value="0"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBoxItem IsEnabled="False" Margin="43.5,30.5,748.5,0">
<TextBlock Text="Reason Code" FontSize="32" FontFamily="Helvetica" FontWeight="Bold" Foreground="#ffffff" IsHitTestVisible="False" />
</ListBoxItem>
<ListBoxItem IsEnabled="False">
<Border Margin="43,0,57,0" BorderBrush="#979797" BorderThickness="0,0,0,1" Width="948"/>
</ListBoxItem>
<ListBoxItem>
<TextBlock Text="ATC" Margin="43,0,57,0" Foreground="#FFFFFF" FontSize="32" FontFamily="{ThemeResource Bold}" FontWeight="Normal" SelectionHighlightColor="Blue"></TextBlock>
</ListBoxItem>
<ListBoxItem IsEnabled="False">
<Border Margin="43,0,57,0" BorderBrush="#979797" BorderThickness="0,0,0,1" Width="948"/>
</ListBoxItem>
<ListBoxItem>
<TextBlock Text="Weather" Margin="43,0,57,0" Foreground="#FFFFFF" FontSize="32" FontFamily="{ThemeResource Bold}" FontWeight="Normal"></TextBlock>
</ListBoxItem>
<ListBoxItem IsEnabled="False" Margin="0">
<Border Margin="43,0,57,0" BorderBrush="#979797" BorderThickness="0,0,0,1" Width="948"/>
</ListBoxItem>
<ListBoxItem Height="50">
<TextBox PlaceholderText="Other" Margin="43.5,0,0,0" Tapped="FuelReasonOther_Tapped" TextChanged="TextBox_TextChanged" Foreground="#FF0078D7" FontSize="32" FontFamily="{ThemeResource Bold}" FontWeight="Normal" Style="{StaticResource TransparentTextBox}"/>
</ListBoxItem>
</ListBox>
</Grid>
</Popup>
我想
修改“Other”的前景色以及此处键入的文本,使其以蓝色显示。
我知道我需要使用ContentTemplate
,但不能这样做。如果覆盖TextControl定义的笔刷,则可以根据需要更改颜色,而无需为整个TextBox控件设置主题。我用以下代码对其进行了测试,结果很有效:
<TextBox PlaceholderText="Other" Margin="43.5,0,0,0" FontSize="32" FontWeight="Normal">
<TextBox.Resources>
<SolidColorBrush x:Key="TextControlPlaceholderForeground" Color="LightBlue"/>
<SolidColorBrush x:Key="TextControlPlaceholderForegroundFocused" Color="LightBlue" />
<SolidColorBrush x:Key="TextControlPlaceholderForegroundPointerOver" Color="LightBlue" />
<SolidColorBrush x:Key="TextControlForeground" Color="Blue" />
<SolidColorBrush x:Key="TextControlForegroundFocused" Color="Blue" />
<SolidColorBrush x:Key="TextControlForegroundPointerOver" Color="Blue" />
</TextBox.Resources>
</TextBox>
注意:我删除了绑定和事件处理程序,只保留UI元素。但是您应该能够将TextBox.Resources添加到您的TextBox中,并查看它的工作情况
资源覆盖TextControl(位于TextBox内)的默认笔刷。控件使用这些笔刷名称在特定情况下进行绘制
让我知道这是否起作用。如果默认模板是任何指示器,则其中的占位符ContentControl
绑定到{ThemeResource SystemControlPageTextBaseMediumBrush}
,因此您可以覆盖笔刷
或直接编辑模板。即使我的文本框是绑定到一个样式,我也无法超越它。你能帮我修改代码吗?你已经有了文本框的自定义样式模板了吗?如果是这样的话,那么一定要发布代码。您应该在占位符上看到一个x:Name=“Placeholder textcontentpresenter”
,占位符可以更改的前景。