在绑定的gridview内单击更改XAML

时间:2017-08-23 15:41:28

标签: c# xaml gridview windows-10-universal

我们正在开发一个内部Windows 10应用程序,用于时间跟踪。我们并不是非常熟悉它,但几乎已经建成了。问题是我们如何在gridview中更改单个选定项目的内容。

XAML代码

<Page
    x:Class="xxxxx.AllJobs"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:xxxxx"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid x:Name="gridAllJobs" Background="White">
        <GridView x:Name="gridViewJobs" Margin="5,120,0,0" Grid.Column="1" Background="LightGray">
            <GridView.ItemTemplate>
                <DataTemplate>
                    <StackPanel x:Name="JobPanel" Orientation="Vertical" Background="White" Margin="10,10,0,0" Tapped="JobPanel_Tapped">
                        <local:DefaultContent/>
                    </StackPanel>
                </DataTemplate>
            </GridView.ItemTemplate>
        </GridView>
        <TextBlock HorizontalAlignment="Left" Margin="10,10,0,0" TextWrapping="Wrap" Text="Jobs" VerticalAlignment="Top" Height="63" Width="124" FontSize="48"/>
        <TextBlock x:Name="lblTime" Margin="0,10,10,0" TextWrapping="Wrap" Text="8:00:00" FontSize="36" FontWeight="Bold" TextAlignment="Right" Padding="2,2,2,0" HorizontalAlignment="Right" VerticalAlignment="Top" Width="174" Height="50"/>
        <TextBlock x:Name="txtEmployee" HorizontalAlignment="Right" Margin="0,67,10,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="48" Width="223" FontSize="24" TextAlignment="Right" Padding="2"/>
        <Image x:Name="imgClockOut" HorizontalAlignment="Right" Height="115" Margin="0,0,238,0" VerticalAlignment="Top" Width="115" Source="Assets/clockout.png" Tapped="imgClockOut_Tapped"/>

    </Grid>
</Page>

当用户选择单个作业时,我们希望将其替换为另一个资源(或者可能有更好的方法来执行此操作)。但只是为了那份工作。

DefaultConent是一个usercontrol

<UserControl
    x:Class="xxxxx.DefaultContent"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:xxxxx"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="150"
    d:DesignWidth="400">

        <StackPanel x:Name="OuterPanel" Orientation="Horizontal" Background="White" Margin="20,0,0,0">
            <StackPanel x:Name="LeftPanel" Margin="5" Width="225">
                <TextBlock x:Name="txtProdID"  Text="{Binding ModuleID}" FontSize="24" FontWeight="Bold" Foreground="DarkBlue" Padding="2" FontFamily="Microsoft JhengHei"/>
                <TextBlock x:Name="txtJobID" Text="{Binding JobID}"  Padding="2" FontSize="10"/>
                <TextBlock Text="{Binding ItemID}"  Padding="2" FontWeight="Bold" FontFamily="Microsoft JhengHei" FontSize="20"/>
                <TextBlock Text="{Binding ProductName}" Padding="2" FontSize="12" TextWrapping="WrapWholeWords"/>
            </StackPanel>
            <StackPanel x:Name="DividerPanel" Margin="0,10,0,10" Background="LightGray" Width="1"/>
            <StackPanel x:Name="RightPanel" Orientation="Vertical" Width="95" Margin="20,5,20,10">
                <TextBlock Text="{Binding CalcQty}" TextAlignment="Right" Padding="2" FontSize="24"/>
                <TextBlock Text="{Binding OprID}" TextAlignment="Right" VerticalAlignment="Bottom" Padding="2,38,2,2"/>
            </StackPanel>
        <StackPanel x:Name="JobStatus">
            <local:NotStarted/>
        </StackPanel>
    </StackPanel>
</UserControl>

我们希望用按钮替换该内容。我们对任何想法都完全开放。我们可能会对此完全错误。我确信它很简单,我们找不到答案。

提前致谢。

更新

我已经能够在点击时绑定新属性。当我将此属性添加为文本块时,它会在单击时更新。然而,&#34; Setter&#34;似乎没有做任何事情。我的文字的价值发生了变化。但面板的属性没有变化。

<StackPanel x:Name="LeftPanel" Margin="5" Width="225">
                                <TextBlock x:Name="txtProdID"  Text="{Binding ModuleID}" FontSize="24" FontWeight="Bold" Foreground="DarkBlue" Padding="2" FontFamily="Microsoft JhengHei"/>
                                <TextBlock x:Name="txtJobID" Text="{Binding JobID}"  Padding="2" FontSize="10"/>
                                <TextBlock Text="{Binding ItemID}"  Padding="2" FontWeight="Bold" FontFamily="Microsoft JhengHei" FontSize="20"/>
                                <TextBlock Text="{Binding ProductName}" Padding="2" FontSize="12" TextWrapping="WrapWholeWords"/>
                                <TextBlock Text="{Binding IsClicked}" Padding="2" FontSize="12" TextWrapping="WrapWholeWords"/>
                            </StackPanel>

                            <StackPanel x:Name="DividerPanel" Margin="0,10,0,10" Background="LightGray" Width="1"/>

                            <StackPanel x:Name="RightPanel" Orientation="Vertical" Width="95" Margin="20,5,20,10">
                                <TextBlock Text="{Binding CalcQty}" TextAlignment="Right" Padding="2" FontSize="24"/>
                                <TextBlock Text="{Binding OprID}" TextAlignment="Right" VerticalAlignment="Bottom" Padding="2,38,2,2"/>
                            </StackPanel>

                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup>
                                    <VisualState>
                                        <VisualState.StateTriggers>
                                            <StateTrigger x:Name="CurrItem" IsActive="{Binding Path=IsClicked}"/>
                                        </VisualState.StateTriggers>
                                        <VisualState.Setters>
                                            <Setter Target="LeftPanel.Visibility" Value="Collapsed"/>
                                        </VisualState.Setters>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                        </StackPanel>

1 个答案:

答案 0 :(得分:0)

您的UI未更新的几个原因。

  1. 您可以对绑定应用模式可能会强制恢复数据。

  2. 但更重要的是,您是否实现了INotifyPropertyChanged接口,这允许更新后的属性在更新时发送回ui。

  3. 要执行此操作,请将: INotifyPropertyChanged的界面添加到您的班级,其中包含需要更新的数据,然后系统会提示您添加实施。

    然后更新您的属性以返回该属性。

    示例:

        public class DemoCustomer : INotifyPropertyChanged
            {
        public event PropertyChangedEventHandler PropertyChanged;
    
    //NotifyPropertyChanged
    
        // This method is called by the Set accessor of each property.
                // The CallerMemberName attribute that is applied to the optional propertyName
                // parameter causes the property name of the caller to be substituted as an argument.
                private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
                {
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                    }
                }
    
        //Property:
    
        public string CustomerName
                {
                    get
                    {
                        return this.customerNameValue;
                    }
    
                    set
                    {
                        if (value != this.customerNameValue)
                        {
                            this.customerNameValue = value;
                            NotifyPropertyChanged();
                        }
                    }
                }
        }
    

    参考示例:https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx