【问题标题】:Creating a few controls in code behind failing在失败后的代码中创建一些控件
【发布时间】:2017-02-15 19:28:09
【问题描述】:

当检查某个广播按钮时,我想在 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);
}

【问题讨论】:

  • 出了什么问题?你说你想要什么,而不是你尝试过的结果。
  • 我收到此消息:PresentationCore.dll 中出现“System.InvalidOperationException”类型的未处理异常
  • 请参考我编辑的答案。

标签: c# wpf xaml code-behind


【解决方案1】:

要动态添加单选按钮,您可以使用绑定到集合的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>

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

【讨论】:

    【解决方案2】:

    试试这个设置SliderForeground属性:

    sl.SetResourceReference(Slider.ForegroundProperty, "SliderSelectionRangeBackgroundBrush");
    

    这将LabelContent属性绑定到SliderValue属性:

    label2.SetBinding(Label.ContentProperty, new Binding("Value") { Source = sl });
    

    我收到此消息:PresentationCore.dll 中出现“System.InvalidOperationException”类型的未处理异常

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

    Slider sl;
    Label label1;
    Label label2;
    private void AllFour_Unchecked(object sender, RoutedEventArgs e)
    {
        label1 = new Label();
        label1.HorizontalAlignment = HorizontalAlignment.Center;
    
        sl = new Slider();
        sl.Minimum = 1;
        sl.Maximum = 4;
        sl.TickFrequency = 1;
        sl.IsSnapToTickEnabled = true;
        sl.SetResourceReference(Control.StyleProperty, "SliderStyle");
        sl.SetResourceReference(Slider.ForegroundProperty, "SliderSelectionRangeBackgroundBrush");
    
        label2 = new Label();
        label2.HorizontalAlignment = HorizontalAlignment.Center;
        label2.BorderBrush = new SolidColorBrush(Colors.Gray);
        label2.SetBinding(Label.ContentProperty, new Binding("Value") { Source = sl });
    
        Dispatcher.BeginInvoke(new Action(() =>
        {
            Upgrades.Children.Add(label1);
            Upgrades.Children.Add(sl);
            Upgrades.Children.Add(label2);
        }), System.Windows.Threading.DispatcherPriority.Background);
    }
    
    private void AllFour_Checked(object sender, RoutedEventArgs e)
    {
        Upgrades.Children.Remove(label1);
        Upgrades.Children.Remove(sl);
        Upgrades.Children.Remove(label2);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-23
      • 2019-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多