WPF动态添加具有不同多个文本的按钮

WPF动态添加具有不同多个文本的按钮,wpf,vb.net,button,dynamic,styles,Wpf,Vb.net,Button,Dynamic,Styles,我为按钮创建了一个模板: <Style x:Key="TableButtonStyle" TargetType="{x:Type Button}"> <Setter Property="Background" Value="{StaticResource GrayBlueGradientBrush}" /> <Setter Property="Width" Value="100" /> <

我为按钮创建了一个模板:

        <Style  x:Key="TableButtonStyle"  TargetType="{x:Type Button}">
        <Setter Property="Background" Value="{StaticResource GrayBlueGradientBrush}" />
        <Setter Property="Width" Value="100" />
        <Setter Property="Height" Value="100" />
        <Setter Property="Margin" Value="20" />
        <Setter Property="Template">
                <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid Width="{TemplateBinding Width}"  
                          Height="{TemplateBinding Height}" ClipToBounds="True">
                        <!-- Inner Rectangle with rounded corners. -->
                        <Rectangle x:Name="innerRectangle"  
                            Stroke="Transparent"  
                            StrokeThickness="20"  
                            Fill="{TemplateBinding Background}"  
                            RadiusX="20" RadiusY="20"   />
                        <Ellipse x:Name="NumberGuests"
                            Width="25" 
                            Height="25"   
                            Stretch="Fill" 
                            StrokeLineJoin="Round" 
                            Fill="Red"
                            Margin="-70,-70,0,0"/>
                        <Ellipse x:Name="NumberChecks"
                            Width="25" 
                            Height="25"   
                            Stretch="Fill" 
                            StrokeLineJoin="Round" 
                            Fill="Green"
                            Margin="70,-70,0,0"/>
                        <Rectangle x:Name="Time"  
                            Width="70" 
                            Height="25"
                            Fill="White"  
                            RadiusX="10" RadiusY="10"   
                            Margin="0,50,0,0"/>
                        <TextBlock x:Name='TableID'
                            FontSize="12" 
                            HorizontalAlignment="Center"
                            FontWeight="Bold"
                            Margin="0,25,0,0"
                            Text="Table_New"/>
                        <TextBlock x:Name='TimeID'
                            FontSize="12" 
                            HorizontalAlignment="Center"
                            Margin="0,65,0,0"    
                            Text="Time_New" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsPressed" Value="True">
                <Setter Property="Opacity" Value="0.5" />
            </Trigger>
        </Style.Triggers>
    </Style>


 Private Sub CreateButton_Click(sender As Object, e As RoutedEventArgs)
    Dim TableName As String
    Dim TimeNotAvailable As String

    LayoutGrid.AllowDragging = True
    ToggleButton.Content = "Edit"

    Dim LocationButton As New Button
    Dim style As Style = TryCast(Me.FindResource("TableButtonStyle"), Style)

    TableName = "Test" & I
    TimeNotAvailable = "Time" & I

    I += 1
    LocationButton.Style = style

    TableID.Content = TableName
    TimeID.Content = TimeNotAvailable

    LayoutGrid.Children.Add(LocationButton)
    AddHandler LocationButton.Click, AddressOf LocationButtonClicked
End Sub

Private Sub CreateButton_Click(发送者作为对象,e作为RoutedEventArgs)
将表名设置为字符串
暗淡的时间不可用作字符串
LayoutGrid.AllowDraging=True
ToggleButton.Content=“编辑”
将位置按钮变暗为新按钮
Dim style As style=TryCast(Me.FindResource(“TableButtonStyle”),style)
TableName=“Test”&I
TimeNotAvailable=“Time”&I
I+=1
LocationButton.Style=Style
TableID.Content=TableName
TimeID.Content=TimeNotAvailable
LayoutGrid.Children.Add(LocationButton)
AddHandler LocationButton。单击,LocationButton的地址单击
端接头
每次按下“创建按钮”按钮时,将根据模板生成一个按钮。 但是,我无法设置textblock.text TableID和TimeID。 创建按钮时,我需要tableID和TimeID来获取变量的值。如“表1”、“表2”等

我尝试了所有不同类型的绑定、资源等,但绑定会更改所有按钮中的文本,第一个按钮文本将是“表1”,但生成第二个按钮时,两个按钮都将是“表2”。我还尝试了DataContext,但两个文本块的数据相同

我尝试直接在xaml中创建一个按钮,“TableID.Content=TableName”起作用,但在使用模板时,“TableID”无法识别


请提供一些帮助。谢谢

作为快速解决方案,您可以尝试此解决方法

将绑定添加到文本属性,如下所示

             <TextBlock x:Name='TableID'
                        FontSize="12" 
                        HorizontalAlignment="Center"
                        FontWeight="Bold"
                        Margin="0,25,0,0"
                        Text="{Binding [0], FallbackValue=Table_New}"/>
             <TextBlock x:Name='TimeID'
                        FontSize="12" 
                        HorizontalAlignment="Center"
                        Margin="0,65,0,0"    
                        Text="{Binding [1], FallbackValue=Time_New}" />

上面的示例演示了MVVM在UI上显示数据的方式

解释


{TableName,TimeNotAvailable}
创建一个隐式数组,作为DataContext传递给按钮,然后将其用于与索引器的绑定,其中[0]是第一个元素,[1]是第二个元素。

您尝试过哪些其他类型的绑定?您为什么认为
[0]
[1]
是否在此处处理匿名对象的属性?他们只是为索引器工作。还测试了一个简单的演示,绑定失败。路径应该分别是
TableName
TimeNotAvailable
。@KingKing,我在答案中添加了解释。代码是VB.net,它为提到的代码生成数组。嗯,不确定VB.net是否支持这种语法。(我在c#中测试过)。这是正确的@KingKing VB.net有一些与c#语法冲突的语法。我在发布之前正式测试了上述代码。
TableID.Content = TableName
TimeID.Content = TimeNotAvailable
LocationButton.DataContext = { TableName, TimeNotAvailable }