사용자에게 자산 목록을 보여주고 목록에서 자산을 삭제할 수있는보기 ('Familify'라고 함)를 작성 중입니다. 자산은 ViewModel의 ObservableCollection
에 저장되므로 삭제할 명령은 자산 오브젝트를 가져 와서 콜렉션에서 제거합니다. '삭제'기능을 사용하는 데 문제가 있습니다. 다음은 XAML과 코드 숨김입니다 :사용 방법 x : 데이터 템플릿과 다른 데이터 유형으로 바인딩
Familify.xaml
<ListView
ItemsSource="{Binding Assets}">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80px" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="150px" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="60px" />
</Grid.ColumnDefinitions>
<TextBlock
Grid.Column="0"
Text="{Binding number}" FontFamily="Consolas"/>
<TextBlock
Grid.Column="1"
Text="{Binding type}"/>
<TextBlock
Grid.Column="2"
Text="add binding here"/>
<TextBlock
Grid.Column="3"
Text="add binding here"/>
<Button
Command="{x:Bind ViewModel.RemoveAssetCommand}"
CommandParameter="{Binding}"
Content=""
FontFamily="Segoe MDL2 Assets"
Grid.Column="4">
</Button>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Familify.xaml.cs
namespace asset_manager.Views
{
public sealed partial class Familify : UserControl
{
FamilifyViewModel ViewModel { get; set; }
public Familify()
{
this.InitializeComponent();
DataContextChanged += (s, e) =>
{
ViewModel = DataContext as FamilifyViewModel;
};
}
}
}
는 생각이 버튼을 클릭하면 목록에서 자산을 제거한다는 것입니다 . (그냥 제대로 작동 일반 바인딩 등, number
, type
을 보여주의한다.) 지금까지 내 생각 : 페이지의보기 모델에 저장된 RemoveAssetCommand
에 액세스 할 수 binding
를 사용하는
- 시도합니다. 그러나 조상 바인딩을 사용할 수 없습니다. 즉,
findAncestor
은 UWP의 요소가 아니기 때문에 XAML 계층 구조에서 상위 요소의 데이터 컨텍스트를 찾지 못했습니다. x:Bind
왜냐하면 속성에 대한 명시적인 경로를 사용하기 때문입니다. 따라서, 코드 뒤에서ViewModel
을 선언하면x:Bind ViewModel.property
을 사용할 수 있습니다. 모두 좋고 좋아. 나는 그 일을했으며 IntelliSense를 사용하면 ViewModel.RemoveAssetCommand에 액세스 할 수있었습니다.- 그러나 오류
no DataType defined for DataTemplate
이 발생했기 때문에 이것이 작동하지 않았습니다. 이것은 의미가 있으므로 두 가지를 시도했습니다. - (위의
DataTemplate
태그에 삽입)은 데이터 템플릿에 표시된 모델이므로 먼저 시도해 보았습니다. 물론, 명령은 모델에서 선언되지 않으며,보기 모델에서 선언되었으므로 작동하지 않습니다. - 대신
x:DataType="ViewModels:FamilifyViewModel"
을 시도했지만 그걸로x:Bind
을 사용할 수 있다고 생각했습니다. 그러나 나는Asset
타입의 객체를FamilifyViewModel
으로 던질 수 없다는 오류가 발생했습니다. 이 데이터 템플리트로 전달되는 오브젝트가Asset
유형이기 때문에 이는 의미가 있습니다.
x:Bind
이 작동한다고 생각한 전체 이유는 코드 숨김의 ViewModel에서 직접 속성에 액세스 할 수 있기 때문입니다.
명시 적으로 1) ViewModel
에서 기본 레벨 속성 (이 경우 Prism 명령)에 액세스하려면 데이터 템플릿에서 x:Bind
을 사용할 수 있습니까? 2)이 기능을 구현하는 데 더 좋은 방법이 있습니까?
이 완벽, 감사합니다! –