gpt4 book ai didi

C# WPF - Not able to show text(C#WPF-无法显示文本)

转载 作者:bug小助手 更新时间:2023-10-27 20:25:04 29 4
gpt4 key购买 nike



I've followed this guide on youtube: https://www.youtube.com/watch?v=_p2K4P9df8g

我在Youtube上关注了这个指南:https://www.youtube.com/watch?v=_p2K4P9df8g


in the guide these navbuttons are created
<local:NavButton Navlink="/Pages/Page3.xaml" Margin="3" Padding="6" Icon=" remove for readability "/>

在本指南中,创建了这些导航按钮


                <Border x:Name="back" CornerRadius="8" Padding="{TemplateBinding Padding}" Background="Transparent" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<Path x:Name="icon" Height="18" Width="18" Stretch="Uniform" Fill="#7b8792" Data="{TemplateBinding Icon}"/>
</Border>

I've already created this in NavButton.cs, but i can't seem to get the text visable, any help would be greatly appriciated.

我已经在NavButton.cs中创建了这个,但我似乎无法使文本可见,任何帮助都将非常有用。


    public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(NavButton), new PropertyMetadata(null));

更多回答
优秀答案推荐

To make the text visible, you'll need to add a text element (e.g., a TextBlock) within your NavButton control's XAML template. Here's an example of how you can modify your XAML to include the text:

要使文本可见,您需要在NavButton控件的XAML模板中添加一个文本元素(例如,TextBlock)。下面是一个如何修改XAML以包含文本的示例:


<Style TargetType="local:NavButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:NavButton">
<Grid>
<!-- Add your text here -->
<TextBlock Text="{TemplateBinding Text}" HorizontalAlignment="Center" VerticalAlignment="Center"/>

<Border x:Name="back" CornerRadius="8" Padding="{TemplateBinding Padding}" Background="Transparent" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<Path x:Name="icon" Height="18" Width="18" Stretch="Uniform" Fill="#7b8792" Data="{TemplateBinding Icon}"/>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style



In this modified template, a TextBlock element is added within the Grid to display the text. The Text property of the TextBlock is bound to the Text property of your NavButton. This way, when you set the Text property in your NavButton control, it will be displayed as text within the button.

在这个修改后的模板中,在Grid中添加了一个TextBlock元素来显示文本。TextBlock的Text属性绑定到NavButton的Text属性。这样,当您在NavButton控件中设置Text属性时,它将显示为按钮内的文本。


Now, when you use your NavButton control and set the Text property, the text should be visible on the button. For example:

现在,当您使用NavButton控件并设置Text属性时,按钮上的文本应该是可见的。例如:


<local:NavButton Navlink="/Pages/Page3.xaml" Margin="3" Padding="6" Icon="YourIconData" Text="Button Text"/>

Make sure to replace "YourIconData" with your actual icon data and "Button Text" with the text you want to display on the button.

请确保将“YourIconData”替换为您的实际图标数据,并将“Button Text”替换为您希望在按钮上显示的文本。


更多回答

Thanks for the explanation, works like a charm now

谢谢你的解释,现在很管用

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com