DataTemplat
e가 ComboBox
이고 일부가 TextBox
인 ListView
입니다. ComboBox
은 CollectionViewSource에 매핑되는 Collection에 바인딩됩니다. ListView
은 여러 행을 가질 수 있습니다.컬렉션을 ListView의 ComboBox에 바인딩합니다.
문제는 하나의 ComboBox
에서 항목을 선택하면 그들 모두를 변경한다는 것입니다. 나는 그들 모두가 동일한 내용으로 채워지기를 원하지만 독립적으로 설정할 수 있기를 바란다.
자료 섹션에는 다음이 포함
ListView
는 다음과 같이 정의된다
<CollectionViewSource Source="{Binding ChildAccounts}" x:Key="ChildGroupedData">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="group"/>
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
<!-- Template for each child item in ListView -->
<DataTemplate x:Key="ChildTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="90"/>
<ColumnDefinition Width="130"/>
<ColumnDefinition Width="90"/>
<ColumnDefinition Width="90"/>
<ColumnDefinition Width="90"/>
<ColumnDefinition Width="210"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Content="Account" HorizontalAlignment="Left" VerticalAlignment="Top" Foreground="{StaticResource CustomWhite}" FontSize="14" Width="80"/>
<ComboBox Grid.Column="1" SelectedValue="{Binding Path=accFrom}" ItemsSource="{Binding Source={StaticResource ChildGroupedData}}" ItemTemplate="{StaticResource AccountTemplate}" SelectedValuePath="ID" Width="120" Style="{StaticResource RoundedComboBox}" HorizontalAlignment="Left" VerticalAlignment="Center">
<ComboBox.GroupStyle>
<GroupStyle ContainerStyle="{StaticResource CustomExpanderComboGroupItemStyle}" HeaderTemplate="{StaticResource GroupHeader}"/>
</ComboBox.GroupStyle>
</ComboBox>
<Label Grid.Column="2" Content="Amount" HorizontalAlignment="Left" VerticalAlignment="Top" Foreground="{StaticResource CustomWhite}" FontSize="14" Width="80"/>
<TextBox Grid.Column="3" Text="{Binding Path=amount, StringFormat='#,##0.00'}" Style="{StaticResource RoundedTextBox}" Width="80" HorizontalAlignment="Left" VerticalAlignment="Center"/>
<Label Grid.Column="4" Content="Comment" HorizontalAlignment="Left" VerticalAlignment="Top" Foreground="{StaticResource CustomWhite}" FontSize="14" Width="80"/>
<TextBox Grid.Column="5" Text="{Binding Path=comment}" Style="{StaticResource RoundedTextBox}" Width="200" HorizontalAlignment="Left" VerticalAlignment="Center"/>
</Grid>
</DataTemplate>
<!-- ListView template -->
<Style x:Key="ChildListViewStyle" TargetType="{x:Type ListView}">
<Setter Property="ItemTemplate" Value="{DynamicResource ChildTemplate}"/>
<Setter Property="Background" Value="{StaticResource CustomBackground}"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="Margin" Value="10,10,10,10"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="Padding" Value="0,0,50,0"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Style.Resources>
<!-- Makes selection invisible when focus lost -->
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="{StaticResource CustomBackgroundC}"/>
</Style.Resources>
</Style>
:
<ListView Grid.Column="0" x:Name="lstChildren" Margin="20,30,0,0" ItemsSource="{Binding Path=Items}" Style="{StaticResource ChildListViewStyle}"/>
편집 :
다음은 연관된 클래스에서 발견되는 다음
Imports System.Data
Imports System.Data.OleDb
Imports System.Collections.ObjectModel
Imports System.ComponentModel
Class ItemView
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Private Sub NotifyPropertyChanged(ByVal info As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
End Sub
....
Private _ChildAccounts As ObservableCollection(Of AccountEntry)
Public Property ChildAccounts As ObservableCollection(Of AccountEntry)
Get
Return _ChildAccounts
End Get
Set(value As ObservableCollection(Of AccountEntry))
_ChildAccounts = value
End Set
End Property
....
Private Sub ItemView_Initialized(sender As Object, e As EventArgs) Handles Me.Initialized
Dim dsacmd As OleDbDataAdapter
Dim dsa As New DataSet
Dim dva As DataView
Dim strSelect As String
Try
' ** Open a connection to the database.
cn = New OleDbConnection(strConnection)
cn.Open()
Me.DataContext = Me
strSelect = "SELECT Accounts.ID, Accounts.deleted, Accounts.accType, Accounts.currency as curr, IIf([Currencies.ID]=1,Accounts.comment,Accounts.comment & "" ("" & Currencies.symbol & "")"") AS comment, Currencies.comment AS currS FROM Currencies INNER JOIN Accounts ON Currencies.ID = Accounts.currency"
dsacmd = New OleDbDataAdapter(strSelect, cn)
dsacmd.Fill(dsa, "Accounts")
dva = New DataView(dsa.Tables("Accounts"))
dva.RowFilter = "accType=" & cVirtual.ToString & " AND deleted=False"
dva.Sort = "curr, comment"
ChildAccounts = New ObservableCollection(Of AccountEntry)(dva.ToTable.AsEnumerable().[Select](Function(i) New [AccountEntry](i("ID"), i("currS").TrimEnd(" "), i("comment"))))
....
Private Sub DisplayItem()
....
strSelect = ""
Dim Relations As Collection(Of Relation) = GetRelations(ID)
For Each r As Relation In Relations
strSelect &= "ID=" & r.ID.ToString & " OR "
Next
If strSelect <> "" Then strSelect = "SELECT * FROM Items WHERE " & strSelect.Substring(0, strSelect.Length - 4)
If strSelect <> "" Then
dsrcmd = New OleDbDataAdapter(strSelect, cn)
dsr.Clear()
dsrcmd.Fill(dsr, "Items")
lstChildren.DataContext = dsr
End If
....
감사합니다. 첫 번째 시도하고 작동하지 않습니다.콤보 상자는 비어 있습니다. 초를 컴파일 할 수 없습니다. '대상 유형에서'ChildAccounts '멤버를 찾을 수 없습니다.'및 'l : ChildAccounts 멤버가 유효한 유형 이름이 없기 때문에 멤버가 유효하지 않습니다. {x : Static l : ItemView.ChildAccounts}로 변경했는데 하나의 오류 만 발생합니다. 'ItemView'유형의 'ChildAccounts'멤버를 찾을 수 없습니다. 네임 스페이스에 대한 액세스를 허용하기 위해 xmlns : l = "clr-namespace : WpfAccounts"를 선언했습니다. 코드에서 ChildAccounts를 포함하는 클래스를 ItemView라고합니다. 감사. Andy –
@AndyPowell 첫 번째 방법은 저에게 잘 작동합니다. ChuldAccounts 속성과 Items 속성 모두에 대한보기 모델 코드를 게시 할 수 있습니까? 필요한 경우 코드를 자르십시오. 네임 스페이스와 클래스 구조를 유지하십시오. 아쉽게도 Dropbox에 대한 제 작업 솔루션을 게시하여 조금만 시도해보십시오. ListView에 바인딩 된 속성 Items는 관찰 가능한 ItemView 클래스 개체의 컬렉션입니까? – Viv
@AndyPowell 옵션 1이 작동하는 샘플 프로젝트를 다운로드 할 링크가있는 옵션 1 이후에 내 대답을 업데이트했습니다. 다운로드하여 VM 속성 선언이 다른지 확인하십시오. 대답에서 언급 한 옵션 2는 정적 변수에 대한 것입니다. 속성이 정적이 아니라면 사용할 수 없습니다. – Viv