1

我有一个 wpf 应用程序,其中有四个单选按钮。

我想根据正在检查的单选框更改列表视图的内容。

我已经定义了数据模板,但找不到将它们分配给单选框选中事件的列表视图的方法。以下是数据模板。

        <Window.Resources>

    <DataTemplate x:Key="OneTimeDataTemplate">
        <GroupBox Height="160" HorizontalAlignment="Left" Margin="104,23,0,0" Name="gbOneTime" VerticalAlignment="Top" Width="453">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="36*" />
                    <ColumnDefinition Width="418*" />
                </Grid.ColumnDefinitions>
                <DatePicker Height="25" HorizontalAlignment="Left" Margin="32,17,0,0" Name="datePicker1" VerticalAlignment="Top" Width="115" Grid.Column="1" />
                <toolkit:TimePicker Height="28" Format="ShortTime" Margin="0,16,146,109" HorizontalAlignment="Right" Width="105" Grid.Column="1"></toolkit:TimePicker>
                <Label Content="Start:" Height="28" HorizontalAlignment="Left" Margin="20,16,0,0" Name="lblOneTimeStart" VerticalAlignment="Top" Grid.ColumnSpan="2" />
                <CheckBox Content="Active" Height="16" HorizontalAlignment="Left" Margin="278,21,0,0" Name="cbOneTimeActive" VerticalAlignment="Top" Grid.Column="1" />
            </Grid>
        </GroupBox>
    </DataTemplate>

    <DataTemplate x:Key="DailyDataTemplate">
        <GroupBox  Height="104" HorizontalAlignment="Left" Margin="105,21,0,0" Name="gbDaily" VerticalAlignment="Top" Width="448">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="36*" />
                    <ColumnDefinition Width="418*" />
                </Grid.ColumnDefinitions>
                <toolkit:TimePicker Format="ShortTime" Margin="13,7,0,63" Grid.Column="1" HorizontalAlignment="Left" Width="105" />
                <toolkit:TimePicker Format="ShortTime" Margin="0,9,145,61" Grid.Column="1" HorizontalAlignment="Right" Width="105" />
                <Label Content="From:" Height="28" HorizontalAlignment="Left" Margin="6,6,0,0" Name="lblDailyFrom" VerticalAlignment="Top" Grid.ColumnSpan="2" />
                <Label Content="To:" Height="28" HorizontalAlignment="Left" Margin="122,8,0,0" Name="lblDailyTo" VerticalAlignment="Top" Grid.Column="1" />
                <Label Content="Run every :" Grid.ColumnSpan="2" Height="28" HorizontalAlignment="Left" Margin="6,51,0,0" Name="lblDailyRunEvery" VerticalAlignment="Top" />
                <Label Content="days" Height="28" HorizontalAlignment="Left" Margin="114,51,0,0" Name="lblDailyDays" VerticalAlignment="Top" Grid.Column="1" />
                <TextBox Height="23" Text="1" HorizontalAlignment="Left" Margin="53,53,0,0" Name="textBox3" VerticalAlignment="Top" Width="53" Grid.Column="1" />
                <CheckBox Content="Active" Height="16" HorizontalAlignment="Left" Margin="286,13,0,0" Name="cbDailyActive" VerticalAlignment="Top" Grid.Column="1" />
            </Grid>
        </GroupBox>

    </DataTemplate>

    <DataTemplate x:Key="WeeklyDataTemplate">
        <GroupBox  Height="160" HorizontalAlignment="Left" Margin="117,17,0,0" Name="gbWeekly" VerticalAlignment="Top" Width="436" Grid.ColumnSpan="2">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="36*" />
                    <ColumnDefinition Width="418*" />
                </Grid.ColumnDefinitions>
                <toolkit:TimePicker Format="ShortTime" Grid.Column="1" HorizontalAlignment="Left" Margin="17,11,0,115" Width="105" />
                <toolkit:TimePicker Format="ShortTime" Grid.Column="1" HorizontalAlignment="Right" Margin="0,13,141,115" Width="105" />
                <Label Content="From:" Grid.ColumnSpan="2" Height="28" HorizontalAlignment="Left" Margin="10,10,0,0" Name="label4" VerticalAlignment="Top" />
                <Label Content="Run every :" Grid.ColumnSpan="2" Height="28" HorizontalAlignment="Left" Margin="10,55,0,0" Name="label6" VerticalAlignment="Top" />
                <Label Content="week on:" Grid.Column="1" Height="28" HorizontalAlignment="Left" Margin="98,55,0,0" Name="label7" VerticalAlignment="Top" />
                <TextBox Grid.Column="1" Height="23" Margin="53,60,311,0" Name="textBox3" Text="1" VerticalAlignment="Top" />
                <Label Content="To:" Height="28" HorizontalAlignment="Left" Margin="140,10,0,0" Name="label5" VerticalAlignment="Top" Grid.Column="1" />
                <CheckBox Content="Monday" Height="16" HorizontalAlignment="Left" Margin="13,104,0,0" Name="checkBox1" VerticalAlignment="Top" Grid.ColumnSpan="2" />
                <CheckBox Content="Tuesday" Height="16" HorizontalAlignment="Left" Margin="62,104,0,0" Name="checkBox2" VerticalAlignment="Top" Grid.Column="1" />
                <CheckBox Content="Wednesday" Height="16" HorizontalAlignment="Left" Margin="140,104,0,0" Name="checkBox3" VerticalAlignment="Top" Grid.Column="1" />
                <CheckBox Content="Thursday" Height="16" HorizontalAlignment="Left" Margin="221,104,0,0" Name="checkBox4" VerticalAlignment="Top" Grid.Column="1" />
                <CheckBox Content="Sunday" Height="16" HorizontalAlignment="Left" Margin="140,131,0,0" Name="checkBox5" VerticalAlignment="Top" Grid.Column="1" />
                <CheckBox Content="Friday" Grid.ColumnSpan="2" Height="16" HorizontalAlignment="Left" Margin="13,126,0,0" Name="checkBox6" VerticalAlignment="Top" />
                <CheckBox Content="Saturday" Height="16" HorizontalAlignment="Left" Margin="62,131,0,0" Name="checkBox7" VerticalAlignment="Top" Grid.Column="1" />
                <CheckBox Content="Active" Height="16" HorizontalAlignment="Left" Margin="303,15,0,0" Name="checkBox8" VerticalAlignment="Top" Grid.Column="1" />
            </Grid>
        </GroupBox>
    </DataTemplate>

    <DataTemplate x:Key="MonthlyDataTemplate">
        <GroupBox  Height="160" HorizontalAlignment="Left" Margin="134,34,0,0" Name="gbMonthly" VerticalAlignment="Top" Width="466" Grid.ColumnSpan="2">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="36*" />
                    <ColumnDefinition Width="418*" />
                </Grid.ColumnDefinitions>
                <toolkit:TimePicker Format="ShortTime" Grid.Column="1" HorizontalAlignment="Left" Margin="17,11,0,115" Width="105" />
                <toolkit:TimePicker Format="ShortTime" Grid.Column="1" HorizontalAlignment="Right" Margin="0,13,141,115" Width="105" />
                <Label Content="From:" Grid.ColumnSpan="2" Height="28" HorizontalAlignment="Left" Margin="10,10,0,0" Name="lblMonthlyFrom" VerticalAlignment="Top" />
                <Label Content="To:" Height="28" HorizontalAlignment="Left" Margin="140,10,0,0" Name="lblMonthlyTo" VerticalAlignment="Top" Grid.Column="1" />
                <ComboBox Grid.Column="1" Height="23" HorizontalAlignment="Left" Margin="33,60,0,0" Name="cbMonths" VerticalAlignment="Top" Width="244">
                    <ComboBoxItem Content="January" DataContext="{Binding}" IsSelected="True" />
                    <ComboBoxItem Content="February" />
                    <ComboBoxItem Content="March" />
                    <ComboBoxItem Content="April" />
                    <ComboBoxItem Content="May" />
                    <ComboBoxItem Content="June" />
                    <ComboBoxItem Content="July" />
                    <ComboBoxItem Content="August" />
                    <ComboBoxItem Content="September" />
                    <ComboBoxItem Content="October" />
                    <ComboBoxItem Content="November" />
                    <ComboBoxItem Content="December" />
                </ComboBox>
                <Label Content="Months:" Grid.ColumnSpan="2" Height="28" HorizontalAlignment="Left" Margin="10,58,0,0" Name="label6" VerticalAlignment="Top" />
                <ComboBox Grid.Column="1" Height="23" HorizontalAlignment="Left" Margin="33,94,0,0" Name="comboBox2" VerticalAlignment="Top" Width="244">
                    <ComboBoxItem Content="1" IsSelected="True" />
                    <ComboBoxItem Content="2" />
                    <ComboBoxItem Content="3" />
                </ComboBox>
                <Label Content="Days:" Grid.ColumnSpan="2" Height="28" HorizontalAlignment="Left" Margin="10,92,0,0" Name="label7" VerticalAlignment="Top" />
                <CheckBox Content="Active" Height="16" HorizontalAlignment="Left" Margin="306,15,0,0" Name="checkBox1" VerticalAlignment="Top" Grid.Column="1" />
            </Grid>
        </GroupBox>
    </DataTemplate>

</Window.Resources>
4

2 回答 2

0

您也可以使用触发器来执行此操作

 <Grid>
    <Grid.Resources>
        <Style x:Key="DynamicTemplateStyleList" TargetType="ListView">
            <Style.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource 
                             AncestorType=UserControl}, 
                             Path=DataContext.IsMonthlyChecked}" Value="True">
                     <Setter Property="ItemTemplate" 
                             Value="{StaticResource MonthlyDataTemplate}"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource 
                             AncestorType=UserControl}, 
                             Path=DataContext.IsOneTimeChecked}" Value="True">
                     <Setter Property="ItemTemplate" 
                             Value="{StaticResource OneTimeDataTemplate}"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource 
                             AncestorType=UserControl}, 
                             Path=DataContext.IsWeeklyChecked}" Value="True">
                     <Setter Property="ItemTemplate" 
                             Value="{StaticResource WeeklyDataTemplate}"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Grid.Resources>
    <ListView Name="list" Style="{StaticResource DynamicTemplateStyleList}"/>
</Grid>
于 2013-08-21T19:47:23.020 回答
0

您可以尝试该代码:

 <DockPanel>
    <StackPanel Orientation="Horizontal" DockPanel.Dock="Top">
        <RadioButton x:Name="OneTimeRadioButton" Content="OneTime" IsChecked="True" />
        <RadioButton x:Name="DailyRadioButton" Content="Daily" />
        <RadioButton x:Name="WeeklyRadioButton" Content="Weekly" />
        <RadioButton x:Name="MonthlyRadionButton" Content="Monthly" />
    </StackPanel>
    <ListView ItemsSource="{Binding Items}">
        <ListView.Resources>
            <Style TargetType="ListView">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=OneTimeRadioButton, Path=IsChecked}" Value="True">
                        <Setter Property="ItemTemplate" Value="{StaticResource OneTimeDataTemplate}" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding ElementName=DailyRadioButton, Path=IsChecked}" Value="True">
                        <Setter Property="ItemTemplate" Value="{StaticResource DailyDataTemplate}" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding ElementName=WeeklyRadioButton, Path=IsChecked}" Value="True">
                        <Setter Property="ItemTemplate" Value="{StaticResource WeeklyDataTemplate}" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding ElementName=MonthlyRadionButton, Path=IsChecked}" Value="True">
                        <Setter Property="ItemTemplate" Value="{StaticResource MonthlyDataTemplate}" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ListView.Resources>
    </ListView>
</DockPanel>
于 2013-08-21T19:52:54.097 回答