我有一个UWP项目,我正试图在我的template<typename Tuple>
constexpr auto pop_front(Tuple tuple) {
static_assert(std::tuple_size<Tuple>::value > 0, "Cannot pop from an empty tuple");
return std::apply(
[](auto, auto... rest) { return std::make_tuple(rest...); },
tuple);
}
上设置绑定。
它基于this guide。
我在ValueConverter
上创建了DependencyProperty
,但始终为ValueConverter
,而不是null
类型的元素。
有我的代码:
MainPage.xaml中
Vehicle
MainPage.xaml.cs中
<Page
x:Class="Project.Pages.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:conv="using:Project.Converters"
>
<Page.Resources>
<conv:Item_to_FullItem x:Key="Item_to_FullItem" VehicleItem="{Binding}"/>
</Page.Resources>
<Grid>
<ListView x:Name="ListView_Vehicles" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<ListView.ItemTemplate>
<DataTemplate>
<Grid ScrollViewer.VerticalScrollBarVisibility="Auto">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
</Grid.RowDefinitions>
<Border Grid.Column="0" BorderThickness="1" BorderBrush="Black">
<TextBlock Text="{Binding Path=Category}"/>
</Border>
<Border Grid.Column="1" BorderThickness="1" BorderBrush="Black">
<TextBlock Text="{Binding Item, Converter={StaticResource Item_to_FullItem}}"/>
</Border>
<Border Grid.Column="2" BorderThickness="1" BorderBrush="Black">
<TextBlock Text="{Binding Path=Weight}"/>
</Border>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</Page>
转换器
namespace Project.Pages
{
public sealed partial class MainPage : Page
{
public MainPage()
{
InitializeComponent();
Fill_Vehicle_List();
ListView_Vehicles.ItemsSource = VehicleServices.Vehicles;
}
public class Vehicle : BindableBase
{
private int _Category;
public int Category
{
get { return _Category; }
set { Set(ref _Category, value); }
}
private string _Items;
public string Items
{
get { return _Items; }
set { Set(ref _Items, value); }
}
private double? _Weight;
public double? Weight
{
get { return _Weight; }
set { Set(ref _Weight, value); }
}
}
public class Values_Vehicle : ObservableCollection<Vehicle> { }
public static class VehicleServices
{
public static Values_Vehicle Vehicles = new Values_Vehicle();
static VehicleServices()
{
}
}
public static void Fill_Vehicle_List()
{
VehicleServices.Vehicles.Add(new Vehicle()
{
Category = 1,
Items = "1.0",
Weight = 1000,
});
VehicleServices.Vehicles.Add(new Vehicle()
{
Category = 2,
Items = "1.1",
Weight = 1600,
});
VehicleServices.Vehicles.Add(new Vehicle()
{
Category = 8,
Items = "1.2",
Weight = 1400,
});
VehicleServices.Vehicles.Add(new Vehicle()
{
Category = 13,
Items = "1.3",
Weight = 1500,
});
VehicleServices.Vehicles.Add(new Vehicle()
{
Category = 1,
Items = "2.0",
Weight = 1100,
});
}
}
}
答案 0 :(得分:2)
问题是,当您在{Binding}
部分中使用<Page.Resources>
时,将会相对于Page
{{1}评估绑定}。如果您在DataContext
的构造函数中设置DataContext = new Vehicle()
,则可以验证这一点。
要解决此问题,您只需将转换器移到Page
声明:
DataTemplate
这样<DataTemplate>
<Grid ScrollViewer.VerticalScrollBarVisibility="Auto">
<Grid.Resources>
<converters:Item_to_FullItem x:Key="Item_to_FullItem" VehicleItem="{Binding}"/>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
</Grid.RowDefinitions>
<Border Grid.Column="0" BorderThickness="1" BorderBrush="Black">
<TextBlock Text="{Binding Path=Category}"/>
</Border>
<Border Grid.Column="1" BorderThickness="1" BorderBrush="Black">
<TextBlock Text="{Binding Item, Converter={StaticResource Item_to_FullItem}}"/>
</Border>
<Border Grid.Column="2" BorderThickness="1" BorderBrush="Black">
<TextBlock Text="{Binding Path=Weight}"/>
</Border>
</Grid>
</DataTemplate>
的数据上下文将是当前的Binding
项,它应该按预期工作。