我需要在项目中使用ItemsControl
。我想不仅ItemsControl
中的每个项目都可以自动缩放,而且ItemsControl
中的每个项目都具有相同的大小。
所以我决定用ViewBox
来做。
这是XAML:
<Viewbox Grid.Column="1" VerticalAlignment="Center" StretchDirection="UpOnly">
<ItemsControl Name="FunctionButton" Grid.Column="1" VerticalAlignment="Center">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button>
<Button.Template>
<ControlTemplate TargetType="Button">
<Grid Background="#00008000" Cursor="Hand" Margin="0,5" VerticalAlignment="Center">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.25*"/>
<ColumnDefinition Width="0.15*"/>
<ColumnDefinition Width="0.60*"/>
</Grid.ColumnDefinitions>
<Ellipse Height="{Binding Path=ActualWidth,RelativeSource={RelativeSource Self}}"
Fill="#44474c" Name="E"/>
<TextBlock Text="{Binding ButtonName}"
Grid.Column="2"
VerticalAlignment="Center"
Foreground="#575757"
FontSize="13"
Margin="0,10"/>
</Grid>
<ControlTemplate.Triggers>
<EventTrigger RoutedEvent="Mouse.MouseEnter">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetProperty="(Ellipse.Fill).(SolidColorBrush.Color)"
Storyboard.TargetName="E"
To="#a4cbd0"
Duration="00:00:01"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
<EventTrigger RoutedEvent="Mouse.MouseLeave">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetProperty="(Ellipse.Fill).(SolidColorBrush.Color)"
Storyboard.TargetName="E"
To="#44474c"
Duration="00:00:01"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Viewbox>
这里有代码:
public MainPage()
{
InitializeComponent();
FunctionButton.ItemsSource = FunctionButtonList;
FunctionButtonClass FBC = new FunctionButtonClass();
FBC.ButtonName = "123";
FunctionButtonList.Add(FBC);
FBC = new FunctionButtonClass();
FBC.ButtonName = "4546465";
FunctionButtonList.Add(FBC);
FBC = new FunctionButtonClass();
FBC.ButtonName = "34534534534";
FunctionButtonList.Add(FBC);
}
public class FunctionButtonClass : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChange(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
string _ButtonName;
public string ButtonName
{
get
{
return _ButtonName;
}
set
{
_ButtonName = value;
NotifyPropertyChange("ButtonName");
}
}
}
List<FunctionButtonClass> FunctionButtonList = new List<FunctionButtonClass>();
第三个缺少椭圆,第一个和第二个的椭圆看起来大小不一样。
我的代码出了什么问题?如何使ItemsControl
中的每个项目都相同?谢谢。
答案 0 :(得分:0)
如果你想让任何控件以相同的条件开始,比如身高等,你可以改变你的public abstract class EndlessRecyclerViewScrollListener extends RecyclerView.OnScrollListener {
private final RecyclerView.LayoutManager mLayoutManager;
public static class Manager {
/// start page of the window
private final int startPage = 0;
// The minimum amount of items to have below your current scroll position
// before loading more.
private int visibleThreshold = 5;
private final int window;
private final int perPage;
// end page of the window
private int endPage;
// True if we are still waiting for the last set of data to load.
boolean loadingPrev = false;
boolean loadingNext = false;
public Manager(int window, int perPage, int endPage) {
this.window = window;
this.perPage = perPage;
this.endPage = endPage;
}
@Override
public String toString() {
return "startPage: " + startPage +
" visibleThreshold: " + visibleThreshold +
" window: " + window +
" perPage: " + perPage +
" endPage: " + endPage +
" loadingPrev: " + loadingPrev +
" loadingNext: " + loadingNext;
}
}
private Manager manager;
public EndlessRecyclerViewScrollListener(Manager manager, RecyclerView.LayoutManager layoutManager) {
this.manager = manager;
this.mLayoutManager = layoutManager;
if (layoutManager instanceof GridLayoutManager) {
manager.visibleThreshold = manager.visibleThreshold * ((GridLayoutManager) layoutManager).getSpanCount();
} else if (layoutManager instanceof StaggeredGridLayoutManager) {
manager.visibleThreshold = manager.visibleThreshold * ((StaggeredGridLayoutManager) layoutManager).getSpanCount();
}
}
public int getLastVisibleItem(int[] lastVisibleItemPositions) {
int maxSize = 0;
for (int i = 0; i < lastVisibleItemPositions.length; i++) {
if (i == 0) {
maxSize = lastVisibleItemPositions[i];
} else if (lastVisibleItemPositions[i] > maxSize) {
maxSize = lastVisibleItemPositions[i];
}
}
return maxSize;
}
public int getFirstVisibleItem(int[] firstVisibleItemPositions) {
int minSize = 0;
for (int i = 0; i < firstVisibleItemPositions.length; i++) {
if (i == 0) {
minSize = firstVisibleItemPositions[i];
}
}
return minSize;
}
// This happens many times a second during a scroll, so be wary of the code you place here.
// We are given a few useful parameters to help us work out if we need to load some more data,
// but first we check if we are waiting for the previous load to finish.
@Override
public void onScrolled(RecyclerView view, int dx, int dy) {
int lastVisibleItemPosition = 0;
int firstVisibleItemPosition = 0;
int totalItemCount = mLayoutManager.getItemCount();
if (mLayoutManager instanceof StaggeredGridLayoutManager) {
int[] lastVisibleItemPositions = ((StaggeredGridLayoutManager) mLayoutManager).findLastVisibleItemPositions(null);
// get maximum element within the list
lastVisibleItemPosition = getLastVisibleItem(lastVisibleItemPositions);
int[] firstVisibleItemPositions = ((StaggeredGridLayoutManager) mLayoutManager).findFirstVisibleItemPositions(null);
// get maximum element within the list
firstVisibleItemPosition = getFirstVisibleItem(firstVisibleItemPositions);
} else if (mLayoutManager instanceof GridLayoutManager) {
lastVisibleItemPosition = ((GridLayoutManager) mLayoutManager).findLastVisibleItemPosition();
firstVisibleItemPosition = ((GridLayoutManager) mLayoutManager).findFirstVisibleItemPosition();
} else if (mLayoutManager instanceof LinearLayoutManager) {
lastVisibleItemPosition = ((LinearLayoutManager) mLayoutManager).findLastVisibleItemPosition();
firstVisibleItemPosition = ((LinearLayoutManager) mLayoutManager).findFirstVisibleItemPosition();
}
// If the total item count is zero and the previous isn't, assume the
// list is invalidated and should be reset back to initial state
if (totalItemCount == 0) {
resetState();
}
// If it isn’t currently loading, we check to see if we have breached
// the visibleThreshold and need to reload more data.
// If we do need to reload some more data, we execute onLoadNext to fetch the data.
// threshold should reflect how many total columns there are too
if (!manager.loadingPrev) {
if (dy < 0 && (firstVisibleItemPosition - manager.visibleThreshold) <= 0 && totalItemCount > 0) {
if (manager.endPage > 0) {
manager.endPage--;
}
manager.loadingPrev = true;
manager.loadingNext = false;
onLoadPrev(getCurrentPageFromTop(), totalItemCount, view);
}
}
if (!(manager.loadingNext)) {
if (dy > 0 && (lastVisibleItemPosition + manager.visibleThreshold) > totalItemCount) {
manager.endPage++;
manager.loadingNext = true;
manager.loadingPrev = false;
onLoadNext(manager.endPage, totalItemCount, view);
}
}
}
private int getCurrentPageFromTop() {
int value = manager.endPage - manager.window;
if (value < 0) {
value = -1;
}
return value;
}
public int getEndPage() {
return manager.endPage;
}
public final void loadNextComplete() {
manager.loadingNext = false;
}
public final void loadPrevComplete() {
manager.loadingPrev = false;
}
public boolean isLoadingNext() {
return manager.loadingNext;
}
public boolean isLoadingPrev() {
return manager.loadingPrev;
}
// Call this method whenever performing new searches
// FixMe: Come up with something better
public void resetState() {
manager.loadingNext = false;
manager.loadingPrev = false;
}
public abstract void onLoadPrev(int page, int totalItemsCount, RecyclerView view);
// Defines the process for actually loading more data based on page
public abstract void onLoadNext(int page, int totalItemsCount, RecyclerView view);
}
。如果您这样做,每个控件都会获得相同的起始值,如果您不手动更改它。我想你的椭圆应该没问题。
Application.xaml