【问题标题】:Editable Grid In Windows 10 UWP AppWindows 10 UWP 应用程序中的可编辑网格
【发布时间】:2016-05-03 12:51:27
【问题描述】:

我正在使用下面的列表视图来创建产品界面,如何使所选项目在此视图中可编辑。我想将所选行的每个单元格放在 TextBox 而不是 TextBlock 中,以便用户可以编辑值。寻求社区的帮助。

<Grid>
    <StackPanel>
        <ListView Name="ItemsList" ItemsSource="{x:Bind products}">
         <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                    <Setter Property="ContentTemplate" Value="{StaticResource DefaultTemplate}" />
                </Style>
            </ListView.ItemContainerStyle>

        <ListView.ItemTemplate>
            <DataTemplate x:DataType="data:Product">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <Grid.Resources>
                        <Style TargetType="TextBlock">
                            <Setter Property="Margin" Value="5,0" />
                        </Style>
                    </Grid.Resources>
                    <TextBlock Grid.Column="0" Text="{x:Bind ProductId}" />
                    <TextBlock Grid.Column="1" Text="{x:Bind ProductName}" />                        
                    </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    </StackPanel>
</Grid>

【问题讨论】:

  • 您可以尝试在 XAML 中使用 TextBox 而不是 TextBlock,并将 IsReadOnly 属性绑定到 SelectedIndex 之类的东西上吗?
  • 你能分享一下具体是怎么做的示例吗?

标签: c# .net xaml windows-10 uwp


【解决方案1】:

@Jackie 建议一直使用TextBox,在未选中项目时将其设为只读,这是一个很好的建议;它简化了逻辑和布局。 (在可见的TextBox 和可见的TextBlock 之间切换很棘手,因为您希望文本位于完全相同的位置。)

出于说明目的,我将使用这个简化的模型对象:

public sealed class Item : INotifyPropertyChanged
{
    private bool isReadOnly = true;

    public bool IsReadOnly
    {
        get
        {
            return isReadOnly;
        }

        set
        {
            isReadOnly = value;
            OnPropertyChanged();
        }
    }

    public string Value { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

接下来,ListView

<ListView SelectionChanged="OnSelectionChanged">
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextBox
                Text="{Binding Value}"
                IsReadOnly="{Binding IsReadOnly}" />
        </DataTemplate>
    </ListView.ItemTemplate>
    <ListView.Items>
        <local:Item Value="One" />
        <local:Item Value="Two" />
        <local:Item Value="Three" />
        <local:Item Value="Four" />
    </ListView.Items>
</ListView>

最后是代码隐藏,它切换ItemIsReadOnly 属性:

private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
    Item removed = e.RemovedItems.FirstOrDefault() as Item;
    if (removed != null)
    {
        removed.IsReadOnly = true;
    }

    Item added = e.AddedItems.FirstOrDefault() as Item;
    if (added != null)
    {
        added.IsReadOnly = false;
    }
}

【讨论】:

  • 谢谢彼得,它更接近我想要的东西
猜你喜欢
  • 2016-01-24
  • 2018-07-06
  • 2015-11-08
  • 2016-04-05
  • 2018-10-31
  • 2016-08-02
  • 2018-08-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多