728x90
ListView 컨트롤에서 StackPanel를 쓸 경우
여러 개의 StackPanel을 구분하기가 쉽지 않습니다
그럴 때는 함수의 자식 컨트롤에 따라 구분하시면 됩니다
우선 XAML 파일에서 아래와 같이 ListView의 DataTemplate를 작성하고
<StackPanel Height="Auto" Orientation="Vertical" Background="Transparent" MouseUp="StackPanel_MouseUp">
<Rectangle Width="50" Height="50" RadiusX="10" RadiusY="10">
<Rectangle.Fill>
<ImageBrush ImageSource="{Binding ImageData}" />
</Rectangle.Fill>
</Rectangle>
<TextBlock Text="{Binding Title}" Height="20" Margin="0,5" Foreground="Black" Background="Transparent" HorizontalAlignment="Center" VerticalAlignment="Bottom"/>
</StackPanel>
아래와 같이 cs 파일에서 특정 데이터를 Binding 합니다
MovieData []mydata = new MyData[m_iCount];
for (int iCnt = 0; iCnt < m_iCount; iCnt++)
{
mydata[iCnt] = new MyData { Title = "Test" + (iCnt + 1).ToString(), ImageData = LoadImage(@"...") };
}
listView1.ItemsSource = mydata;
이렇게 작성 하면 StackPanel에 다중의 ImageBrush와 TextBlock이 생성됩니다
이후 cs 파일에서 MouseUp 이벤트를 등록하고
private void StackPanel_MouseUp(object sender, MouseButtonEventArgs e)
{
StackPanel sp = (StackPanel)sender;
MessageBox.Show(((TextBlock)sp.Children[1]).Text)
}
위와 같이 작성해주면 등록된 TextBlock의 Text를 가져올 수 있습니다
Children의 경우 0번부터 시작이며 0번의 경우 ImageBrush 컨트롤이 존재하고
이후 1번이 TextBlock 이므로 Children[1]을 선택하면 됩니다
728x90
'Application > C# (WPF)' 카테고리의 다른 글
[C# WPF] X / Y 좌표로 Margin 위치 구하기 (0) | 2023.01.13 |
---|---|
[C# WPF] 마우스 올렸을때 변화 주기 (IsMouseOver Trigger) (0) | 2023.01.12 |
[C# WPF] 함수에서 다중 인자 값 넘겨 받기 (0) | 2023.01.11 |
[C# WPF] 파일의 아이콘을 추출해서 BitmapImage 변환하기 (0) | 2023.01.10 |
[C# WPF] 배경화면 투명화 (0) | 2023.01.05 |