访问数据模板中元素的属性

时间:2018-02-20 12:55:38

标签: c# wpf xaml data-binding

如何从c#访问以下XAML代码中的Textblock(txtlink)的属性?引用txtlink。不起作用,导致错误

  

当前上下文中不存在名称txtlink

        <Grid Grid.Row="1">
        <ListBox  ItemsSource="{Binding CurrentRss.Channel.NewsItems}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="80"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>

                        <Button Style="{StaticResource ImageButtonStyle}" Click="Button_Click" >
                            <Image Source="{Binding Image}"/>
                        </Button>
                        <TextBlock Grid.Column="1" Text="{Binding Title}"/>

                        <TextBlock x:Name="txtlink"  Text="{Binding Link}" Background="Black" Foreground="#FFD1DA0B"/>

                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>

1 个答案:

答案 0 :(得分:0)

使用SelectedItem绑定,您将可以访问所选项目。从那里您可以访问Link属性 xaml:

 library(shiny)
    library(DT)

    server <- function(input, output, session){
      myData <- reactive({
        inFile <- input$file1
        if (is.null(inFile)) return(NULL)
        data <- read.csv(inFile$datapath, header = TRUE)
        data
      })

      output$contents <- DT::renderDataTable({
        DT::datatable(myData())       
      })

      output$summary <- renderPrint({
          summary(myData())
      })

      output$select <- renderUI({
        df <- myData()
        selectInput("variable", "Variable:",names(df))


      })


      output$plot <- renderPlot({
        df <- myData()
        df <- df[,input$variable]
        hist(df)
      })

    }


    ui<- shinyUI(fluidPage(
      titlePanel("Uploading Files"),
      sidebarLayout(
        sidebarPanel(
          fileInput('file1', 'Choose CSV File',
                    accept=c('text/csv',
                             'text/comma-separated-values,text/plain',
                             '.csv')),
          uiOutput('select')
        ),
        mainPanel(
          DT::dataTableOutput('contents'),
          verbatimTextOutput('summary'),
          plotOutput('plot')
        )
      )
    )
    )

    shinyApp(ui,server)

并在您的ViewModel中:

<ListBox  ItemsSource="{Binding CurrentRss.Channel.NewsItems}" SelectedItem="{Binding SelectedNewsItem}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="80"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>

                    <Button Style="{StaticResource ImageButtonStyle}" Click="Button_Click" >
                        <Image Source="{Binding Image}"/>
                    </Button>
                    <TextBlock Grid.Column="1" Text="{Binding Title}"/>

                    <TextBlock x:Name="txtlink"  Text="{Binding Link}" Background="Black" Foreground="#FFD1DA0B"/>

                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>