홈>
앙케이트 앱이 있다고 가정 해 봅시다.이 앱은 각각 Label과 ListBox로 구성된 컨트롤 목록이있는 ItemsControl로 구성됩니다. 각 ListBox의 항목은 확인란 또는 라디오 버튼 또는 기타 항목입니다.
내 질문은 : 확인란을 선택하면 확인란이 어떤 질문에 적용되는지 어떻게 알 수 있습니까? Tag 속성에 질문에 대한 참조를 넣어야합니까? 그렇다면 어떻게해야합니까?
아래의 태그 바인딩 코드가 작동하지 않습니다. ListBoxItem에 바인딩합니다. ItemsControl 항목에 어떻게 바인딩합니까?
MainWindow.xaml :
<Window x:Class="ListWithinListTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<ResourceDictionary>
<Style x:Key="ConditionCheckBoxListStyle" TargetType="{x:Type ListBox}">
<Setter Property="SelectionMode" Value="Multiple" />
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="{x:Type ListBoxItem}" >
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<CheckBox IsChecked="{Binding IsSelected,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}"
Click="CheckBoxClicked"
Tag="{Binding RelativeSource={RelativeSource TemplatedParent}}"
>
<ContentPresenter></ContentPresenter>
</CheckBox>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</Window.Resources>
<Grid>
<ItemsControl Name="QuizControl" ItemsSource="{Binding QuizQuestions}" ScrollViewer.CanContentScroll="False">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Margin="10 0 10 10" VerticalAlignment="Top">
<Label Content="{Binding Text}" />
<ListBox ItemsSource="{Binding Options}"
DisplayMemberPath="Text"
Tag="{Binding RelativeSource={RelativeSource AncestorType=ItemsControl}}"
Style="{StaticResource ConditionCheckBoxListStyle}"
/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</Window>
MainWindow.xaml.cs :
using System.Windows;
using System.Windows.Controls;
namespace ListWithinListTest
{
public class Option
{
public string Text { get; set; }
public bool IsSelected { get; set; } = false;
}
public class Question
{
public string Text { get; set; }
public Option[] Options { get; set; }
}
public class ViewModel
{
public Question[] QuizQuestions { get; set; }
public ViewModel()
{
QuizQuestions = new Question[] {
new Question { Text = "How are you?", Options = new Option[] { new Option { Text = "Good" }, new Option { Text = "Fine" } } },
new Question { Text = "How's your dog?", Options = new Option[] { new Option { Text = "Sleepy" }, new Option { Text = "Hungry" } } },
};
}
}
public partial class MainWindow : Window
{
private ViewModel viewModel;
public MainWindow()
{
InitializeComponent();
this.DataContext = viewModel = new ViewModel();
}
private void CheckBoxClicked(object sender, RoutedEventArgs e)
{
Question question = viewModel.QuizQuestions[???];
}
}
}
- 답변 # 1
- 답변 # 2
QuizQuestions를 QuizControl에 바인딩했으며 ItemsSource 속성에서 다시 가져올 수 있습니다.
var questions = (Question[]) QuizControl.ItemsSource;
수정
원래의 질문에 제안하고 싶은 또 다른 방법 인 답을 직접 얻은 것 같습니다 :
옵션 클래스에 속성을 하나 더 생성하십시오
public class Option { public string Text { get; set; } public bool IsSelected { get; set; } = false; public int Index{ get; set; } }
각 질문 옵션에 색인을 추가하십시오.
QuizQuestions = new Question[] { new Question { Text = "How are you?", Options = new Option[] { new Option { Text = "Good", Index = 0 }, new Option { Text = "Fine", Index = 0 } } }, new Question { Text = "How's your dog?", Options = new Option[] { new Option { Text = "Sleepy", Index = 1 }, new Option { Text = "Hungry", Index = 1 } } }, };
CheckBox 이벤트에서 옵션 인덱스를 얻을 수 있습니다
private void CheckBoxClicked(object sender, RoutedEventArgs e) { var s = (CheckBox)sender; var op = (Option)s.Tag; Question question = viewModel.QuizQuestions[op.Index]; }
관련 질문
- c# : WPF에서 창 표시 이벤트?
- xaml - WPF 애플리케이션으로 글꼴을 가져올 수 없습니다
- visual studio - wpf의 xaml 파일에서 xmlns, xmlns - x, xmlns : d, xmlns : mc, xmlns : local, mc : ignorable의 목적은 무엇입니까?
- c# - 기능이 버튼으로 트리거되면 WPF 바인딩이 작동하지 않습니다
- c# - LiveCharts를 사용하여 LineSeries에서 값 속성을 바인딩하는 방법
- c# - for 루프 중에 WPF 그리드 너비가 변경되지 않는 이유
- xaml - wpf - 사전에 바인딩 된 각 콤보 상자 항목의 고유 스타일
- c# - CoreWebView2는 동일한 프로젝트의 다른 XAML 창에서 액세스 할 때 null입니다
- c# - ToggleButton 및 연결된 속성을 사용한 ListBox ItemTemplate 업데이트
- c# - ViewModel의 개체 속성에 바인딩 또는 속성 이름이 같은 여러 모델을 사용하는 방법
좋아, 제거 과정에서 ListBox가 Question이 바인딩 될 것임을 깨달을 때까지 Live Visual Tree 창을 엉망으로 만들었습니다. 나는 그것이 큰 일이라는 것을 알고 있지만, 이것이 내가 여기있는 곳입니다. 그런 다음 RelativeSource AncestorType을 사용하여 질문을 찾고 DataSource 속성을 찾았습니다.
MainWindow.xaml :
MainWindow.xaml.cs :