C# UWP GridViewItem显示类名
我想在UWP中实现一个C# UWP GridViewItem显示类名,c#,xaml,gridview,uwp,datatemplate,C#,Xaml,Gridview,Uwp,Datatemplate,我想在UWP中实现一个GridView,每个项目都包含一个TextBlock,复选框和两个组合框。 我现在得到的是GridViewItem的类名instad。 我认为这可以通过DataTemplate解决,也许 Xaml文件: <Page.Resources> <DataTemplate x:Name="CameraSettingsGridViewTemplate" x:DataType="local:CameraSetting"> <Gri
GridView
,每个项目都包含一个TextBlock
,复选框和两个组合框
。
我现在得到的是GridViewItem
的类名instad。
我认为这可以通过DataTemplate
解决,也许
Xaml文件:
<Page.Resources>
<DataTemplate x:Name="CameraSettingsGridViewTemplate" x:DataType="local:CameraSetting">
<GridViewItem>
<StackPanel>
<TextBlock Text="{x:Bind Property}"/>
<CheckBox/>
<ComboBox ItemsSource="{x:Bind Value1}"/>
<ComboBox ItemsSource="{x:Bind Value2}"/>
</StackPanel>
</GridViewItem>
</DataTemplate>
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<GridView Name="CameraSettings" HorizontalAlignment="Left" VerticalAlignment="Top" ItemsSource="{x:Bind CameraSettingsList}"></GridView>
</Grid>
public sealed partial class CameraSettingsPage : Page {
private ObservableCollection<CameraSetting> cameraSettingsList = new ObservableCollection<CameraSetting>();
public ObservableCollection<CameraSetting> CameraSettingsList { get { return this.cameraSettingsList; } }
public CameraSettingsPage() {
this.InitializeComponent();
this.cameraSettingsList = new ObservableCollection<CameraSetting>() {
new CameraSetting() {
Property = "prop1",
Auto = new CheckBox(),
Value1 = new ComboBox() { ItemsSource = new ObservableCollection<string>() { "1", "2" } },
Value2 = new ComboBox() { ItemsSource = new ObservableCollection<string>() { "a", "b" } }
}
};
}
private void Page_Loaded(object sender, RoutedEventArgs e) {
}
}
public class CameraSetting {
public string Property { get; set; }
public CheckBox Auto { get; set; }
public ComboBox Value1 { get; set; }
public ComboBox Value2 { get; set; }
}
在构造函数中,我在ObservableCollection
中创建了一个示例GridViewItem
,并将其作为GridView
的ItemSource
,这里发生了一些事情。首先,GridView显示类的名称,因为它实际上没有使用您尝试设置的数据模板
您需要对资源使用x:Key
而不是x:Name
,然后将该资源设置为GridView的ItemTemplate
属性:
<Page.Resources>
<DataTemplate x:Key="CameraSettingsGridViewTemplate" x:DataType="local:CameraSetting">
<GridViewItem>
<StackPanel>
<TextBlock Text="{x:Bind Property}"/>
<CheckBox IsChecked="{x:Bind Auto}"/>
<ComboBox ItemsSource="{x:Bind Value1}"/>
<ComboBox ItemsSource="{x:Bind Value2}"/>
</StackPanel>
</GridViewItem>
</DataTemplate>
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<GridView Name="CameraSettings" HorizontalAlignment="Left" VerticalAlignment="Top" ItemsSource="{x:Bind CameraSettingsList}" ItemTemplate="{StaticResource CameraSettingsGridViewTemplate}"></GridView>
</Grid>
第二个问题是不在数据模型中重新创建控件。绑定和模板的要点是,您可以在模型中包含基本类型(如字符串选项值和布尔值),然后将它们绑定到操作它们的UI元素。因此,您的数据模型应该更像这样:
public class CameraSetting
{
public string Property { get; set; }
public bool Auto { get; set; }
public List<string> Value1 { get; set; }
public List<string> Value2 { get; set; }
}
公共类摄像机设置
{
公共字符串属性{get;set;}
公共bool Auto{get;set;}
公共列表值1{get;set;}
公共列表值2{get;set;}
}
然后您可以填写它,类似于您之前的内容:
public ObservableCollection<CameraSetting> CameraSettingsList { get; set; } = new ObservableCollection<CameraSetting>();
public MainPage()
{
this.InitializeComponent();
this.CameraSettingsList.Add(new CameraSetting()
{
Property = "prop1",
Auto = true,
Value1 = new List<string>() { "Option 1", "Option 2" },
Value2 = new List<string>() { "Option 3", "Option 4" },
});
}
公共ObservableCollection CameraSettingsList{get;set;}=new ObservableCollection();
公共主页()
{
this.InitializeComponent();
this.CameraSettingsList.Add(new CameraSetting())
{
Property=“prop1”,
自动=真,
Value1=新列表(){“选项1”,“选项2”},
Value2=新列表(){“选项3”,“选项4”},
});
}
我建议您看看平台提供的一些功能,对这些场景和您在官方网站上遇到的问题有一个很好的概述
请记住,如果您希望在模型更改时更新数据或在用户更改时将数据反映回您的模型,则需要在x:Bind表达式中添加一个
Mode=one-way
或Mode=two-way
。这里有一些事情要做。首先,GridView显示类的名称,因为它实际上没有使用您尝试设置的数据模板
您需要对资源使用x:Key
而不是x:Name
,然后将该资源设置为GridView的ItemTemplate
属性:
<Page.Resources>
<DataTemplate x:Key="CameraSettingsGridViewTemplate" x:DataType="local:CameraSetting">
<GridViewItem>
<StackPanel>
<TextBlock Text="{x:Bind Property}"/>
<CheckBox IsChecked="{x:Bind Auto}"/>
<ComboBox ItemsSource="{x:Bind Value1}"/>
<ComboBox ItemsSource="{x:Bind Value2}"/>
</StackPanel>
</GridViewItem>
</DataTemplate>
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<GridView Name="CameraSettings" HorizontalAlignment="Left" VerticalAlignment="Top" ItemsSource="{x:Bind CameraSettingsList}" ItemTemplate="{StaticResource CameraSettingsGridViewTemplate}"></GridView>
</Grid>
第二个问题是不在数据模型中重新创建控件。绑定和模板的要点是,您可以在模型中包含基本类型(如字符串选项值和布尔值),然后将它们绑定到操作它们的UI元素。因此,您的数据模型应该更像这样:
public class CameraSetting
{
public string Property { get; set; }
public bool Auto { get; set; }
public List<string> Value1 { get; set; }
public List<string> Value2 { get; set; }
}
公共类摄像机设置
{
公共字符串属性{get;set;}
公共bool Auto{get;set;}
公共列表值1{get;set;}
公共列表值2{get;set;}
}
然后您可以填写它,类似于您之前的内容:
public ObservableCollection<CameraSetting> CameraSettingsList { get; set; } = new ObservableCollection<CameraSetting>();
public MainPage()
{
this.InitializeComponent();
this.CameraSettingsList.Add(new CameraSetting()
{
Property = "prop1",
Auto = true,
Value1 = new List<string>() { "Option 1", "Option 2" },
Value2 = new List<string>() { "Option 3", "Option 4" },
});
}
公共ObservableCollection CameraSettingsList{get;set;}=new ObservableCollection();
公共主页()
{
this.InitializeComponent();
this.CameraSettingsList.Add(new CameraSetting())
{
Property=“prop1”,
自动=真,
Value1=新列表(){“选项1”,“选项2”},
Value2=新列表(){“选项3”,“选项4”},
});
}
我建议您看看平台提供的一些功能,对这些场景和您在官方网站上遇到的问题有一个很好的概述
请记住,如果您希望在模型更改时更新数据或在用户更改时将数据反映回您的模型,则需要在x:Bind表达式中添加
Mode=one-way
或Mode=two-way
。感谢您提供的解决方案,如我所愿,并感谢您提供的建议页面:)您好,是否可以根据属性的更改来更新视图?例如,如果我将组合框
的IsEnabled
属性绑定到Auto属性,那么Auto复选框
启用/禁用组合框意味着什么。我在我的CameraSettings
类中添加了INotifyPropertyChanged
功能,但它没有更新。我还将Mode=TwoWay
属性添加到复选框
和组合框
INotifyPropertyChanged
或dependencProperty
中,请参见示例中使用的类:是的,很抱歉,我纠正了一个愚蠢的错误--谢谢:)谢谢您的解决方案,如我所愿工作,感谢Sugested页面阅读:)您好,是否可以根据属性的更改更新视图?例如,如果我将组合框
的IsEnabled
属性绑定到Auto属性,那么Auto复选框
启用/禁用组合框意味着什么。我在我的CameraSettings
类中添加了INotifyPropertyChanged
功能,但它没有更新。我还将Mode=TwoWay
属性添加到复选框
和组合框
INotifyPropertyChanged
或dependencProperty
中,请参见示例中使用的类:是的,很抱歉我纠正了错误,但我犯了一个愚蠢的错误--谢谢:)