0

I made a list view, and i used a data Template as you seeing below and now i want to select the Which item,the user selected and put the text of the selected item textblock to a variable

<ListView.ItemTemplate>

    <DataTemplate >

            <Border CornerRadius="20"
                BorderThickness="2"
                BorderBrush="Red"
                Width="70"
                Height="70"
                Margin="5,5">

            <Button x:Name="calender_btn"
                Background="Transparent"
                    BorderBrush="Transparent"
                    Style="{StaticResource calender_btn_style}"
                    Click="calender_btn_Click">

                <TextBlock x:Name="calender_txt_block"
                           Text="{Binding _outputdate}"
                           Foreground="White"
                           Width="55"
                           FontSize="14"
                           Margin="3,5"
                           TextWrapping="Wrap"
                           VerticalAlignment="Center"
                           HorizontalAlignment="Center"
                           TextAlignment="Center"
                           >

                </TextBlock>

                <Button.Resources>

                    <Style TargetType="Border">

                        <Setter Property="CornerRadius" Value="20"/>

                    </Style>

                </Button.Resources>

            </Button>

            </Border>
        
    </DataTemplate>

</ListView.ItemTemplate>

<ListView.ItemsPanel>
    <ItemsPanelTemplate>
        <WrapPanel/>
    </ItemsPanelTemplate>
</ListView.ItemsPanel>

I have no idea how I can do it, because I am new in wpf, if you know how I can do it.

oguzhancerit
  • 1,436
  • 1
  • 16
  • 27

2 Answers2

0

Depending on how your data looks like, you probably get a data object to each button that contains the text. You can find that object in the buttons data context.

    private void calender_btn_Click(object sender, RoutedEventArgs e)
    {
        var dataObject = ((Button)sender).DataContext;
    }
RolandJS
  • 717
  • 5
  • 5
0

If you are using ItemsSource you can get the selected value by binding to ListView SelectedItem (SelectedValue in some cases).

SelectedItem="{Binding SelectedData, Mode=TwoWay}" 

Assuming that you have SelectedData property in your DataContext with same type as your data in the shown collection.

Google for MVVM