在wpf mvvm中切换选项卡后如何刷新选项卡内容

时间:2019-02-07 12:22:36

标签: c# wpf mvvm

我正在尝试构建一个包含选项卡的WPF窗口。是否可以在选择其他选项卡时刷新选项卡页面的控件(例如文本框,datagridview),所以当我返回上一个选项卡时,我不会想要查看任何数据,而是刷新控件。我想使用MVVM wpf实现此目标

在放置应用程序时,我用表数据填充了数据网格,并且我有两个文本框,可以通过它们选择日期范围。我不知道如何刷新文本框和数据网格以使其在选项卡开关上处于初始状态。

我尝试使用window.Interactivity dll刷新数据。但是没有事件触发过。

           <i:Interaction.Triggers>
                <i:EventTrigger EventName="MouseLeftButtonDown">
                    <i:InvokeCommandAction Command="{Binding ontabclick}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>

ViewModel.cs:

  ontabclick = new RelayCommand(o => onclick());
  public void onclick()
    { MessageBox.Show("Working correctly"); }

Window.xaml

 xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

  <TabItem Header="AuditInformation">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="MouseLeftButtonDown">
                    <i:InvokeCommandAction Command="{Binding ontabclick}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
            <UniformGrid>
                <DockPanel>
                    <StackPanel Margin="1,0,0,0" Height="55" >
                    <DatePicker Focusable = "False"  SelectedDate="{Binding FromDate}" Margin="80,10,30,-10"  Height="25" HorizontalAlignment="Left"  Name="Fromdatedatepicker"   VerticalAlignment="Top" Width="115" >
                            <DatePicker.Resources>
                                <Style TargetType="{x:Type DatePickerTextBox}">
                                    <Setter Property="Control.Template">
                                        <Setter.Value>
                                            <ControlTemplate>
                                                <TextBox x:Name="PART_TextBox" 
                                Text="{Binding Path=SelectedDate, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}, StringFormat={}{0:yyyy/MM/dd}}" />
                                            </ControlTemplate>
                                        </Setter.Value>
                                    </Setter>
                                </Style>
                            </DatePicker.Resources>
                        </DatePicker>

                        <Label Width="69" Height="23.277" HorizontalAlignment="Left" Margin="1,-10,30,0" VerticalAlignment="Top" Content="From Date"/>
                        <DatePicker Focusable = "False" SelectedDate="{Binding Todate}" Margin="265,-27,30,-10" Height="25" HorizontalAlignment="Left"  Name="Todatedatepicker" VerticalAlignment="Top" Width="115" >
                            <DatePicker.Resources>
                                <Style TargetType="{x:Type DatePickerTextBox}">
                                    <Setter Property="Control.Template">
                                        <Setter.Value>
                                            <ControlTemplate>
                                                <TextBox x:Name="PART_TextBox1" 
                                Text="{Binding Path=SelectedDate, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}, StringFormat={}{0:yyyy/MM/dd}}" />
                                            </ControlTemplate>
                                        </Setter.Value>
                                    </Setter>
                                </Style>
                            </DatePicker.Resources>
                        </DatePicker>

                        <Label Width="60" Height="23.277" HorizontalAlignment="Left" Margin="200,-23,30,0" VerticalAlignment="Top" Content="To Date"/>
                        <Button  Command="{Binding dataSearch}" Margin="300,-20,30,0" Width="60" Height="23.277"  Content="Search"></Button>

                    </StackPanel>
                </DockPanel>
                <DockPanel Margin="0,0,0,-139">
                    <StackPanel Height="350">
                        <DataGrid x:Name="datagrid"  AutoGenerateColumns="True" CanUserAddRows="False"
                            Height="350" Width="365" Margin="5" HorizontalAlignment="Left">
                            <DataGrid.Style>
                                <Style TargetType="DataGrid">
                                    <Setter Property="ItemsSource" Value="{Binding ModelClasstab}"/>
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding Isdatementioned}" Value="True">
                                            <Setter Property="ItemsSource" Value="{Binding ModelClasstabwithdate   }"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </DataGrid.Style>
                        </DataGrid>

                    </StackPanel>
                </DockPanel>
            </UniformGrid>
        </TabItem>

1 个答案:

答案 0 :(得分:0)

您可以绑定SelectedIndex的{​​{1}}或SelectedItem属性。在视图模型中,您可以刷新所需的控件。

TabControl

在您的视图模型中

<TabControl SelectedIndex="{Binding SelectedIndex}">
    <TabItem Header="One" >
        <TextBox Text="{Binding TextOne}" />
    </TabItem>
    <TabItem Header="Two" >
        <TextBox Text="{Binding TextTwo}" />
    </TabItem>
</TabControl>