根据您的要求,您可以将RelativePanel 宽度与指定的Source 绑定。我创建了一个用于管理项目宽度的设置类。然后在Application.Resources中初始化一个设置实例。
public class Setting : INotifyPropertyChanged
{
private double _itemWidth = 200;
public double ItemWidth
{
get { return _itemWidth; }
set { _itemWidth = value; OnPropertyChanged(); }
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Application.Resources
<Application.Resources>
<ResourceDictionary>
<local:Setting x:Key="Setting"/>
</ResourceDictionary>
</Application.Resources>
用法
<GridView x:Name="gridView1" ItemsSource="{x:Bind Orders}" ItemClick="GridView1_ItemClick" IsItemClickEnabled="True">
<GridView.ItemTemplate>
<DataTemplate x:DataType="x:String">
<RelativePanel Width="{Binding ItemWidth,Source={StaticResource Setting}}" Background="SeaGreen" >
<TextBlock Text="{x:Bind }"/>
</RelativePanel>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
如果要更新项目宽度,可以在按钮单击事件中修改设置宽度属性。
private void GridView1_ItemClick(object sender, ItemClickEventArgs e)
{
((Setting)Application.Current.Resources["Setting"]).ItemWidth = 500;
}
更新
移至页面资源
<Page.Resources>
<local:Setting x:Key="Setting"/>
</Page.Resources>
用法
private void GridView1_ItemClick(object sender, ItemClickEventArgs e)
{
((Setting)this.Resources["Setting"]).ItemWidth = 500;
}