C# 如何管理XAML中嵌入在按钮中的文本块?
我开发了一个通用应用程序,其中有一些基于“联系人页面”演示的视图,以显示联系人或公司的详细信息 对于每个项目,我在第一个文本块中显示“标签”,在另一个文本块中显示“数据” 例如,此页面显示公司的详细信息: XAML非常简单:C# 如何管理XAML中嵌入在按钮中的文本块?,c#,button,win-universal-app,textblock,launcher,C#,Button,Win Universal App,Textblock,Launcher,我开发了一个通用应用程序,其中有一些基于“联系人页面”演示的视图,以显示联系人或公司的详细信息 对于每个项目,我在第一个文本块中显示“标签”,在另一个文本块中显示“数据” 例如,此页面显示公司的详细信息: XAML非常简单: <ScrollViewer> <StackPanel> <!-- 1. Company name --> <TextBlock Margin="0,9,0,0
<ScrollViewer>
<StackPanel>
<!-- 1. Company name -->
<TextBlock Margin="0,9,0,0"
Text="Company"
Foreground="{StaticResource ThemeBrush}"
Style="{StaticResource ListViewItemContentTextBlockStyle}"/>
<TextBlock Text="{Binding Company.name}"
Style="{StaticResource ListViewItemTextBlockStyle}" />
<!-- 2. Phone -->
<TextBlock Margin="0,9,0,0"
Text="Phone"
Foreground="{StaticResource ThemeBrush}"
Style="{StaticResource ListViewItemContentTextBlockStyle}"/>
<TextBlock Text="{Binding Company.phone}"
Style="{StaticResource ListViewItemTextBlockStyle}"
FontWeight="Normal" />
<!-- 3. Fax -->
<TextBlock x:Uid="commonTextblockFax"
Margin="0,9,0,0"
Text="Fax"
Foreground="{StaticResource ThemeBrush}"
Style="{StaticResource ListViewItemContentTextBlockStyle}"/>
<TextBlock Text="{Binding Company.fax}"
Style="{StaticResource ListViewItemTextBlockStyle}" />
<!-- 4. Location -->
<TextBlock Text="Location"
Margin="0,9,0,0"
Foreground="{StaticResource ThemeBrush}"
Style="{StaticResource ListViewItemContentTextBlockStyle}" />
<TextBlock Text="{Binding Company.address_line_1}"
Style="{StaticResource ListViewItemTextBlockStyle}"
FontWeight="Normal" />
<TextBlock Text="{Binding Company.address_line_2}"
Style="{StaticResource ListViewItemTextBlockStyle}"
FontWeight="Normal" />
<TextBlock Style="{StaticResource ListViewItemTextBlockStyle}"
FontWeight="Normal" >
<Run Text="{Binding Company.postal}"/>
<Run Text="{Binding Company.city}" />
</TextBlock>
<!-- 5. Website-->
<TextBlock Margin="0,9,0,0"
Text="Website"
Foreground="{StaticResource ThemeBrush}"
Style="{StaticResource ListViewItemContentTextBlockStyle}" />
<TextBlock Text="{Binding Company.website}"
Style="{StaticResource ListViewItemTextBlockStyle}"
FontWeight="Normal" />
</StackPanel>
</ScrollViewer>
但其中一些信息必须绑定到启动器上:
<ScrollViewer>
<StackPanel>
<!-- 1. Company -->
<TextBlock Margin="0,9,0,0"
Text="Company"
Foreground="{StaticResource ThemeBrush}"
Style="{StaticResource ListViewItemContentTextBlockStyle}"/>
<TextBlock Text="{Binding Company.name}"
Style="{StaticResource ListViewItemTextBlockStyle}" />
<!-- 2. Phone -->
<TextBlock Margin="0,9,0,0"
Text="Phone"
Foreground="{StaticResource ThemeBrush}"
Style="{StaticResource ListViewItemContentTextBlockStyle}"/>
<Button Margin="0,0,0,-10">
<Button.Template>
<ControlTemplate TargetType="Button">
<ContentPresenter />
</ControlTemplate>
</Button.Template>
<Button.Flyout>
<MenuFlyout>
<MenuFlyout.MenuFlyoutPresenterStyle>
<Style TargetType="MenuFlyoutPresenter">
<Setter Property="Background" Value="{StaticResource ThemeBrush}"/>
</Style>
</MenuFlyout.MenuFlyoutPresenterStyle>
<MenuFlyoutItem Text="Call"
Tag="call"
Command="{Binding PhoneCallCommand}"/>
<MenuFlyoutItem Text="Send SMS"
Tag="SMS"
Command="{Binding PhoneSmsCommand}"/>
</MenuFlyout>
</Button.Flyout>
<TextBlock Text="{Binding Company.phone}"
Style="{StaticResource ListViewItemTextBlockStyle}"
FontWeight="Normal" />
</Button>
<!-- 3. Fax -->
<TextBlock x:Uid="commonTextblockFax"
Margin="0,9,0,0"
Text="Fax"
Foreground="{StaticResource ThemeBrush}"
Style="{StaticResource ListViewItemContentTextBlockStyle}"/>
<TextBlock Text="{Binding Company.fax}"
Style="{StaticResource ListViewItemTextBlockStyle}" />
<!-- 4. Location -->
<TextBlock Text="Location"
Margin="0,9,0,0"
Foreground="{StaticResource ThemeBrush}"
Style="{StaticResource ListViewItemContentTextBlockStyle}"/>
<Button Margin="0,0,0,-10"
Command="{Binding MapCommand}" >
<Button.Template>
<ControlTemplate TargetType="Button">
<ContentPresenter />
</ControlTemplate>
</Button.Template>
<StackPanel>
<TextBlock Text="{Binding Company.address_line_1}"
Style="{StaticResource ListViewItemTextBlockStyle}"
FontWeight="Normal" />
<TextBlock Text="{Binding Company.address_line_2}"
Style="{StaticResource ListViewItemTextBlockStyle}"
FontWeight="Normal" />
<TextBlock Style="{StaticResource ListViewItemTextBlockStyle}"
FontWeight="Normal" >
<Run Text="{Binding Company.postal}"/>
<Run Text="{Binding Company.city}" />
</TextBlock>
</StackPanel>
</Button>
<!-- 5. Website -->
<TextBlock Margin="0,9,0,0"
Text="Website"
Foreground="{StaticResource ThemeBrush}"
Style="{StaticResource ListViewItemContentTextBlockStyle}" />
<Button Margin="0,0,0,-10"
Command="{Binding LinkCommand}" >
<Button.Template>
<ControlTemplate TargetType="Button">
<ContentPresenter />
</ControlTemplate>
</Button.Template>
<TextBlock Text="{Binding Company.website}"
Style="{StaticResource ListViewItemTextBlockStyle}"
FontWeight="Normal" />
</Button>
</StackPanel>
</ScrollViewer>
- 电话号码:显示一个菜单,可在通话或短信之间进行选择
- 电子邮件:启动电子邮件应用程序
- 位置:启动地图应用程序
<ScrollViewer>
<StackPanel>
<!-- 1. Company -->
<TextBlock Margin="0,9,0,0"
Text="Company"
Foreground="{StaticResource ThemeBrush}"
Style="{StaticResource ListViewItemContentTextBlockStyle}"/>
<TextBlock Text="{Binding Company.name}"
Style="{StaticResource ListViewItemTextBlockStyle}" />
<!-- 2. Phone -->
<TextBlock Margin="0,9,0,0"
Text="Phone"
Foreground="{StaticResource ThemeBrush}"
Style="{StaticResource ListViewItemContentTextBlockStyle}"/>
<Button Margin="0,0,0,0">
<Button.Template>
<ControlTemplate TargetType="Button">
<ContentPresenter />
</ControlTemplate>
</Button.Template>
<Button.Flyout>
<MenuFlyout>
<MenuFlyout.MenuFlyoutPresenterStyle>
<Style TargetType="MenuFlyoutPresenter">
<Setter Property="Background" Value="{StaticResource ThemeBrush}"/>
</Style>
</MenuFlyout.MenuFlyoutPresenterStyle>
<MenuFlyoutItem Text="Call"
Tag="call"
Command="{Binding PhoneCallCommand}"/>
<MenuFlyoutItem Text="Send SMS"
Tag="SMS"
Command="{Binding PhoneSmsCommand}"/>
</MenuFlyout>
</Button.Flyout>
<TextBlock Text="{Binding Company.phone}"
Style="{StaticResource ListViewItemTextBlockStyle}"
FontWeight="Normal" />
</Button>
<!-- 3. Fax -->
<TextBlock x:Uid="commonTextblockFax"
Margin="0,9,0,0"
Text="Fax"
Foreground="{StaticResource ThemeBrush}"
Style="{StaticResource ListViewItemContentTextBlockStyle}"/>
<TextBlock Text="{Binding Company.fax}"
Style="{StaticResource ListViewItemTextBlockStyle}" />
<!-- 4. Location -->
<TextBlock Text="Location"
Margin="0,9,0,0"
Foreground="{StaticResource ThemeBrush}"
Style="{StaticResource ListViewItemContentTextBlockStyle}"/>
<Button Margin="0,0,0,0"
Command="{Binding MapCommand}" >
<Button.Template>
<ControlTemplate TargetType="Button">
<ContentPresenter />
</ControlTemplate>
</Button.Template>
<StackPanel>
<TextBlock Text="{Binding Company.address_line_1}"
Style="{StaticResource ListViewItemTextBlockStyle}"
FontWeight="Normal" />
<TextBlock Text="{Binding Company.address_line_2}"
Style="{StaticResource ListViewItemTextBlockStyle}"
FontWeight="Normal" />
<TextBlock Style="{StaticResource ListViewItemTextBlockStyle}"
FontWeight="Normal" >
<Run Text="{Binding Company.postal}"/>
<Run Text="{Binding Company.city}" />
</TextBlock>
</StackPanel>
</Button>
<!-- 5. Website -->
<TextBlock Margin="0,9,0,0"
Text="Website"
Foreground="{StaticResource ThemeBrush}"
Style="{StaticResource ListViewItemContentTextBlockStyle}" />
<Button Margin="0,0,0,0"
Command="{Binding LinkCommand}" >
<Button.Template>
<ControlTemplate TargetType="Button">
<ContentPresenter />
</ControlTemplate>
</Button.Template>
<TextBlock Text="{Binding Company.website}"
Style="{StaticResource ListViewItemTextBlockStyle}"
FontWeight="Normal" />
</Button>
</StackPanel>
</ScrollViewer>
=>发射器工作正常:
但是现在我的物品之间有一个“缺口”,正如我们可以看到的“电话号码”和“传真”标签之间的差距:
我试图通过在按钮上添加一个负边距(“0,0,0,-10”)来修复它:
<ScrollViewer>
<StackPanel>
<!-- 1. Company -->
<TextBlock Margin="0,9,0,0"
Text="Company"
Foreground="{StaticResource ThemeBrush}"
Style="{StaticResource ListViewItemContentTextBlockStyle}"/>
<TextBlock Text="{Binding Company.name}"
Style="{StaticResource ListViewItemTextBlockStyle}" />
<!-- 2. Phone -->
<TextBlock Margin="0,9,0,0"
Text="Phone"
Foreground="{StaticResource ThemeBrush}"
Style="{StaticResource ListViewItemContentTextBlockStyle}"/>
<Button Margin="0,0,0,-10">
<Button.Template>
<ControlTemplate TargetType="Button">
<ContentPresenter />
</ControlTemplate>
</Button.Template>
<Button.Flyout>
<MenuFlyout>
<MenuFlyout.MenuFlyoutPresenterStyle>
<Style TargetType="MenuFlyoutPresenter">
<Setter Property="Background" Value="{StaticResource ThemeBrush}"/>
</Style>
</MenuFlyout.MenuFlyoutPresenterStyle>
<MenuFlyoutItem Text="Call"
Tag="call"
Command="{Binding PhoneCallCommand}"/>
<MenuFlyoutItem Text="Send SMS"
Tag="SMS"
Command="{Binding PhoneSmsCommand}"/>
</MenuFlyout>
</Button.Flyout>
<TextBlock Text="{Binding Company.phone}"
Style="{StaticResource ListViewItemTextBlockStyle}"
FontWeight="Normal" />
</Button>
<!-- 3. Fax -->
<TextBlock x:Uid="commonTextblockFax"
Margin="0,9,0,0"
Text="Fax"
Foreground="{StaticResource ThemeBrush}"
Style="{StaticResource ListViewItemContentTextBlockStyle}"/>
<TextBlock Text="{Binding Company.fax}"
Style="{StaticResource ListViewItemTextBlockStyle}" />
<!-- 4. Location -->
<TextBlock Text="Location"
Margin="0,9,0,0"
Foreground="{StaticResource ThemeBrush}"
Style="{StaticResource ListViewItemContentTextBlockStyle}"/>
<Button Margin="0,0,0,-10"
Command="{Binding MapCommand}" >
<Button.Template>
<ControlTemplate TargetType="Button">
<ContentPresenter />
</ControlTemplate>
</Button.Template>
<StackPanel>
<TextBlock Text="{Binding Company.address_line_1}"
Style="{StaticResource ListViewItemTextBlockStyle}"
FontWeight="Normal" />
<TextBlock Text="{Binding Company.address_line_2}"
Style="{StaticResource ListViewItemTextBlockStyle}"
FontWeight="Normal" />
<TextBlock Style="{StaticResource ListViewItemTextBlockStyle}"
FontWeight="Normal" >
<Run Text="{Binding Company.postal}"/>
<Run Text="{Binding Company.city}" />
</TextBlock>
</StackPanel>
</Button>
<!-- 5. Website -->
<TextBlock Margin="0,9,0,0"
Text="Website"
Foreground="{StaticResource ThemeBrush}"
Style="{StaticResource ListViewItemContentTextBlockStyle}" />
<Button Margin="0,0,0,-10"
Command="{Binding LinkCommand}" >
<Button.Template>
<ControlTemplate TargetType="Button">
<ContentPresenter />
</ControlTemplate>
</Button.Template>
<TextBlock Text="{Binding Company.website}"
Style="{StaticResource ListViewItemTextBlockStyle}"
FontWeight="Normal" />
</Button>
</StackPanel>
</ScrollViewer>
但与没有按钮的第一个版本相比,始终存在差距:
=>有什么方法可以正确执行此操作吗?您可以自定义按钮的样式,并定义自己的内容模板 这样你就可以完全控制它的内容
是按钮开始时的默认样式。非常全面且写得很好的问题!您使用按钮而不仅仅是文本块的
点击事件
是否有原因?这不是真正的原因,但我不知道是否有可能显示此事件的菜单?此外,我还想处理将数据分解为许多文本块的情况,比如从“位置”(address-line1、address-line2、postal、city),而不必将绑定复制到命令?