访问 ListView UWP 中每个 ListViewItem 的 DataTemplate 中的 RelativePanel Windows 10
Accessing RelativePanel in DataTemplate of every ListViewItem in ListView UWP Windows 10
我想访问和更改其 DataTemplate 中每个 ListViewItem 中 RelativePanel 的 FlowDirection。我试过这个确切的方法,有同样的错误:How do I access a control inside a XAML DataTemplate? 我也试过给出的解决方案,但我有
_Container = MyFlipView.ItemContainerGenerator.ContainerFromItem(item);
总是 return 为空,即使我的 item
不为空。
我尝试将 UpdateLayout() 放在它之前,但没有成功。我尝试放置 await
Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>{ }
绕着它,也是徒劳,总是returning null。
这是我的代码:
<ListView x:Name="MessagesListView">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate x:DataType="data:Message">
<RelativePanel x:Name="RelativeDataTemplate" Background="White" Margin="10,5,10,5" MaxHeight="115" MinHeight="115" MinWidth="400" HorizontalAlignment="Stretch">
<TextBlock x:Name="MessageToBlock" Text="{x:Bind MessageTo}" FontSize="14" TextAlignment="DetectFromContent" Foreground="Black" FontWeight="SemiBold" RelativePanel.RightOf="ImageEllipse" Height="20" Margin="10,30,10,20"/>
<TextBlock x:Name="AgentNameBlock" Text="{x:Bind AgentName}" Padding="10,0" TextAlignment="DetectFromContent" RelativePanel.RightOf="ImageEllipse" RelativePanel.Below="MessageToBlock" FontSize="14" Foreground="#2d73b5" FontWeight="SemiBold" Height="20"/>
<TextBlock x:Name="MessageDateBlock" Text="{x:Bind MessageDate}" TextAlignment="Right" FontSize="14" Foreground="Black" RelativePanel.AlignVerticalCenterWith="MessageToBlock" RelativePanel.AlignRightWithPanel="True" Height="20" Margin="0,30,20,20"/>
<TextBlock x:Name="MessageYearBlock" Text="{x:Bind MessageYear}" TextAlignment="Right" FontSize="14" Foreground="#2d73b5" RelativePanel.AlignVerticalCenterWith="AgentNameBlock" RelativePanel.AlignRightWithPanel="True" Margin="0,0,20,0"/>
</RelativePanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
foreach (var item in myListView.Items)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
UpdateLayout();
myListView.ScrollIntoView(item);
var _Container = myListView.ItemContainerGenerator
.ContainerFromItem(item) as FrameworkElement;
var _Children = AllChildren(_Container);
var _RelativePanel = _Children
// only interested in RelativePanel
.OfType<RelativePanel>()
// only interested in RelativePanel
.First(x => x.Name.Equals("RelativeDataTemplate"));
// test & set color
_RelativePanel.FlowDirection = FlowDirection.RightToLeft;
});
}
public List<Control> AllChildren(DependencyObject parent)
{
var _List = new List<Control>();
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
var _Child = VisualTreeHelper.GetChild(parent, i);
if (_Child is Control)
_List.Add(_Child as Control);
_List.AddRange(AllChildren(_Child));
}
return _List;
}
我还没有找到任何解决方案吗?
正如我们所讨论的,您使用 ComboBox
选择语言(流向),因此您可以使用数据绑定和转换器来完成此操作,例如:
<Page.Resources>
<local:FlowDirectionConverter x:Key="cvt" />
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ListView x:Name="MessagesListView" ItemsSource="{x:Bind messagelist}">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate x:DataType="local:Message">
<RelativePanel x:Name="RelativeDataTemplate" Background="White" Margin="10,5,10,5" MaxHeight="115" MinHeight="115" MinWidth="400" HorizontalAlignment="Stretch" FlowDirection="{Binding ElementName=comboBox, Path=SelectedItem,Converter={StaticResource cvt}}">
<TextBlock x:Name="MessageToBlock" Text="{x:Bind MessageTo}" FontSize="14" TextAlignment="DetectFromContent" Foreground="Black" FontWeight="SemiBold" Height="20" Margin="10,30,10,20" />
<TextBlock x:Name="AgentNameBlock" Text="{x:Bind AgentName}" Padding="10,0" TextAlignment="DetectFromContent" RelativePanel.Below="MessageToBlock" FontSize="14" Foreground="#2d73b5" FontWeight="SemiBold" Height="20" />
<TextBlock x:Name="MessageDateBlock" Text="{x:Bind MessageDate}" TextAlignment="Right" FontSize="14" Foreground="Black" RelativePanel.AlignVerticalCenterWith="MessageToBlock" RelativePanel.AlignRightWithPanel="True" Height="20" Margin="0,30,20,20" />
<TextBlock x:Name="MessageYearBlock" Text="{x:Bind MessageYear}" TextAlignment="Right" FontSize="14" Foreground="#2d73b5" RelativePanel.AlignVerticalCenterWith="AgentNameBlock" RelativePanel.AlignRightWithPanel="True" Margin="0,0,20,0" />
</RelativePanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<ComboBox x:Name="comboBox" VerticalAlignment="Bottom" ItemsSource="{x:Bind languagelist}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding language}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Grid>
隐藏代码:
public ObservableCollection<Message> messagelist;
public ObservableCollection<MyLanguage> languagelist = new ObservableCollection<MyLanguage>();
public MainPage()
{
this.InitializeComponent();
messagelist = new ObservableCollection<Message>();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
//add data to messagelist
languagelist.Clear();
languagelist.Add(new MyLanguage { language = "English" });
languagelist.Add(new MyLanguage { language = "Arabic" });
}
MyLanguage class 非常简单:
public class MyLanguage
{
public string language { get; set; }
}
我的转换器是这样的:
public class FlowDirectionConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
if (value != null)
{
var item = value as MyLanguage;
if (item.language == "English")
return FlowDirection.LeftToRight;
else
return FlowDirection.RightToLeft;
}
return FlowDirection.LeftToRight;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
我想访问和更改其 DataTemplate 中每个 ListViewItem 中 RelativePanel 的 FlowDirection。我试过这个确切的方法,有同样的错误:How do I access a control inside a XAML DataTemplate? 我也试过给出的解决方案,但我有
_Container = MyFlipView.ItemContainerGenerator.ContainerFromItem(item);
总是 return 为空,即使我的 item
不为空。
我尝试将 UpdateLayout() 放在它之前,但没有成功。我尝试放置 await
Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>{ }
绕着它,也是徒劳,总是returning null。 这是我的代码:
<ListView x:Name="MessagesListView">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate x:DataType="data:Message">
<RelativePanel x:Name="RelativeDataTemplate" Background="White" Margin="10,5,10,5" MaxHeight="115" MinHeight="115" MinWidth="400" HorizontalAlignment="Stretch">
<TextBlock x:Name="MessageToBlock" Text="{x:Bind MessageTo}" FontSize="14" TextAlignment="DetectFromContent" Foreground="Black" FontWeight="SemiBold" RelativePanel.RightOf="ImageEllipse" Height="20" Margin="10,30,10,20"/>
<TextBlock x:Name="AgentNameBlock" Text="{x:Bind AgentName}" Padding="10,0" TextAlignment="DetectFromContent" RelativePanel.RightOf="ImageEllipse" RelativePanel.Below="MessageToBlock" FontSize="14" Foreground="#2d73b5" FontWeight="SemiBold" Height="20"/>
<TextBlock x:Name="MessageDateBlock" Text="{x:Bind MessageDate}" TextAlignment="Right" FontSize="14" Foreground="Black" RelativePanel.AlignVerticalCenterWith="MessageToBlock" RelativePanel.AlignRightWithPanel="True" Height="20" Margin="0,30,20,20"/>
<TextBlock x:Name="MessageYearBlock" Text="{x:Bind MessageYear}" TextAlignment="Right" FontSize="14" Foreground="#2d73b5" RelativePanel.AlignVerticalCenterWith="AgentNameBlock" RelativePanel.AlignRightWithPanel="True" Margin="0,0,20,0"/>
</RelativePanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
foreach (var item in myListView.Items)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
UpdateLayout();
myListView.ScrollIntoView(item);
var _Container = myListView.ItemContainerGenerator
.ContainerFromItem(item) as FrameworkElement;
var _Children = AllChildren(_Container);
var _RelativePanel = _Children
// only interested in RelativePanel
.OfType<RelativePanel>()
// only interested in RelativePanel
.First(x => x.Name.Equals("RelativeDataTemplate"));
// test & set color
_RelativePanel.FlowDirection = FlowDirection.RightToLeft;
});
}
public List<Control> AllChildren(DependencyObject parent)
{
var _List = new List<Control>();
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
var _Child = VisualTreeHelper.GetChild(parent, i);
if (_Child is Control)
_List.Add(_Child as Control);
_List.AddRange(AllChildren(_Child));
}
return _List;
}
我还没有找到任何解决方案吗?
正如我们所讨论的,您使用 ComboBox
选择语言(流向),因此您可以使用数据绑定和转换器来完成此操作,例如:
<Page.Resources>
<local:FlowDirectionConverter x:Key="cvt" />
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ListView x:Name="MessagesListView" ItemsSource="{x:Bind messagelist}">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate x:DataType="local:Message">
<RelativePanel x:Name="RelativeDataTemplate" Background="White" Margin="10,5,10,5" MaxHeight="115" MinHeight="115" MinWidth="400" HorizontalAlignment="Stretch" FlowDirection="{Binding ElementName=comboBox, Path=SelectedItem,Converter={StaticResource cvt}}">
<TextBlock x:Name="MessageToBlock" Text="{x:Bind MessageTo}" FontSize="14" TextAlignment="DetectFromContent" Foreground="Black" FontWeight="SemiBold" Height="20" Margin="10,30,10,20" />
<TextBlock x:Name="AgentNameBlock" Text="{x:Bind AgentName}" Padding="10,0" TextAlignment="DetectFromContent" RelativePanel.Below="MessageToBlock" FontSize="14" Foreground="#2d73b5" FontWeight="SemiBold" Height="20" />
<TextBlock x:Name="MessageDateBlock" Text="{x:Bind MessageDate}" TextAlignment="Right" FontSize="14" Foreground="Black" RelativePanel.AlignVerticalCenterWith="MessageToBlock" RelativePanel.AlignRightWithPanel="True" Height="20" Margin="0,30,20,20" />
<TextBlock x:Name="MessageYearBlock" Text="{x:Bind MessageYear}" TextAlignment="Right" FontSize="14" Foreground="#2d73b5" RelativePanel.AlignVerticalCenterWith="AgentNameBlock" RelativePanel.AlignRightWithPanel="True" Margin="0,0,20,0" />
</RelativePanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<ComboBox x:Name="comboBox" VerticalAlignment="Bottom" ItemsSource="{x:Bind languagelist}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding language}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Grid>
隐藏代码:
public ObservableCollection<Message> messagelist;
public ObservableCollection<MyLanguage> languagelist = new ObservableCollection<MyLanguage>();
public MainPage()
{
this.InitializeComponent();
messagelist = new ObservableCollection<Message>();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
//add data to messagelist
languagelist.Clear();
languagelist.Add(new MyLanguage { language = "English" });
languagelist.Add(new MyLanguage { language = "Arabic" });
}
MyLanguage class 非常简单:
public class MyLanguage
{
public string language { get; set; }
}
我的转换器是这样的:
public class FlowDirectionConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
if (value != null)
{
var item = value as MyLanguage;
if (item.language == "English")
return FlowDirection.LeftToRight;
else
return FlowDirection.RightToLeft;
}
return FlowDirection.LeftToRight;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}