2017-02-14 53 views
0

當某個單選按鈕被選中時,我想添加或刪除幾個控件到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); 
} 
+0

怎麼回事?你說你想要什麼,但不是你嘗試過的結果。 –

+0

我收到以下消息:在PresentationCore.dll中發生未處理的異常'System.InvalidOperationException' –

+0

請參閱我編輯的答案。 – mm8

回答

0

嘗試使用此方法設置SliderForeground屬性:

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

而這種綁定的Content的財產10至SliderValue屬性:

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

我得到這個消息:「System.InvalidOperationException」類型的未處理的異常出現在PresentationCore.dll中

使用調度員刪除筆錄控制。這是一個可行的完整的例子:

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); 
} 
0

動態添加,您可以使用綁定到一個集合

<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風格移動的ItemsControl單選按鈕,用MainViewModel和每次升級都將是UpgradeViewModel