在失败的代码中创建一些控件

时间:2017-02-14 21:36:22

标签: c# wpf xaml code-behind

我想在选中某个单选按钮时向StackPanel添加或删除一些控件。

我怀疑设置Slider控件的前台绑定是错误的。

MainWindow.xaml

<StackPanel Name ="Upgrades" VerticalAlignment="Center" HorizontalAlignment="Center">
     <RadioButton Name="rb1" Content="Upgrade rb1" />
     <RadioButton Name="rb2" Content="Upgrade rb2" />
     <RadioButton Name="rb3" Content="Upgrade rb3" />
     <RadioButton Name="rb4" Content="Upgrade rb4" IsChecked="True"/>
     <RadioButton Name="AllFour" Content="All Four" Checked="AllFour_Checked" Unchecked="AllFour_Unchecked" />       
     <Button Name="StartUpgrades" Margin="0 0 0 0" Click="StartUpgrades_Click" >Start</Button>
</StackPanel>

<!-- I want to add these controls to the stackpanel before the StartUpgrades Button Control
<Label Name="SelectThreads" HorizontalAlignment="Center">Select Threads</Label>
<Slider  Name="SliderThreadAmount" Minimum="1" Maximum="4" TickFrequency="1" IsSnapToTickEnabled="True" Style="{DynamicResource SliderStyle}" Foreground="{DynamicResource SliderSelectionRangeBackgroundBrush}" IsVisibleChanged="SliderThreadAmount_IsVisibleChanged"></Slider>
<Label HorizontalAlignment="Center" Name="SliderThreadValue" BorderBrush="Gray" Content="{Binding ElementName=SliderThreadAmount,Path=Value}"></Label> -->

MainWindow.xaml.cs

private void AllFour_Unchecked(object sender, RoutedEventArgs e)
{
    Label label1 = new Label();
    label1.HorizontalAlignment = HorizontalAlignment.Center;

    Slider sl = new Slider();
    sl.Minimum = 1;
    sl.Maximum = 4;
    sl.TickFrequency = 1;
    sl.IsSnapToTickEnabled = true;
    sl.SetResourceReference(Control.StyleProperty, "SliderStyle");
    sl.Foreground.SetValue(Control.StyleProperty, "SliderSelectionRangeBackgroundBrush");

    Label label2 = new Label();
    label2.HorizontalAlignment = HorizontalAlignment.Center;
    label2.BorderBrush = new SolidColorBrush(Colors.Gray);
    label2.Content = "{Binding ElementName=sl,Path=Value}";

    Upgrades.Children.Add(label1);
    Upgrades.Children.Add(sl);
    Upgrades.Children.Add(label2);
}

private void AllFour_Checked(object sender, RoutedEventArgs e)
{
    Upgrades.Children.Remove(label1);
    Upgrades.Children.Remove(sl);
    Upgrades.Children.Remove(label2);
}

2 个答案:

答案 0 :(得分:0)

尝试此操作以设置Foreground的<{1}}属性:

Slider

这样可以将sl.SetResourceReference(Slider.ForegroundProperty, "SliderSelectionRangeBackgroundBrush"); 的{​​{1}}属性绑定到Content的{​​{1}}属性:

Label
  

我收到此消息:未处理的类型&#39; System.InvalidOperationException&#39;发生在PresentationCore.dll

使用调度程序删除添加控件。这是一个有效的完整示例:

Value

答案 1 :(得分:0)

要动态添加单选按钮,您可以使用绑定到集合的ItemsControl

<StackPanel Name ="Upgrades" VerticalAlignment="Center" HorizontalAlignment="Center">
     <ItemsControl ItemsSource="{Binding UpgradeButtons}">
           <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <RadioButton Content="{Binding UpgradeName}" IsChecked="{Binding UpgradeChecked}" />
               </DataTemplate>
            </ItemsControl.ItemTemplate>
     </ItemsControl>
     <RadioButton Name="AllFour" Content="All Four" IsChecked="{Binding AllFourSelected}" />       
     <Button Name="StartUpgrades" Margin="0 0 0 0" Command="{Binding StartUpgrades}" />
</StackPanel>

这确实假设使用MainViewModel转向MVVM样式,每次升级都是UpgradeViewModel