我為按鈕制作了樣式
<Style x:Key="ButtonList"
TargetType="Button">
<Setter Property="HorizontalAlignment"
Value="Stretch" />
<Setter Property="Background"
Value="Aqua" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" Padding="5" BorderThickness="1">
<StackPanel VerticalAlignment="Stretch"
HorizontalAlignment="Stretch">
<TextBlock x:Name="AText" />
<TextBlock x:Name="BText" HorizontalAlignment="Right" />
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
如何將“Atext”文本塊和“BText”文本塊的文本與代碼隱藏分開?
uj5u.com熱心網友回復:
為 Button 創建一個 AttachedProperty(或者您可以創建一個基于 Button 的 customControl),以 ContentB 命名。
<TextBlock x:Name="AText" Text={TemplateBinding Content}/>
<TextBlock x:Name="BText" Text={TemplateBinding ap:ContentB} HorizontalAlignment="Right" />
在cs代碼中,您可以訪問 Button.Content 和 Button.ContentB
2.另一種方法:
<TextBlock x:Name="AText" Text={TemplateBinding Content,Converter={StaticSource ContentConverter},ConverterParameter=1}/>
<TextBlock x:Name="BText" Text={TemplateBinding Content,Converter={StaticSource ContentConverter},ConverterParameter=2} HorizontalAlignment="Right" />
ContentConverter.cs 代碼:
internal class ContentConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is string valuestring)
{
return valuestring.Split(',')[(int)parameter-1];
}
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
哦,您需要使用分隔符,例如逗號來分隔兩個文本片段
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/422338.html
標籤:
