2017-02-14 1 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

이 메시지가 나타납니다. 'System.InvalidOperationException'형식의 처리되지 않은 예외가 PresentationCore.dll에서 발생했습니다. –

+0

내 수정 된 답변을 참조하십시오. – mm8

답변

0

이는 SliderForeground 속성을 설정하려고 :

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

을 그리고 이것은 Content 바인딩의 재산

label2.SetBinding(Label.ContentProperty, new Binding("Value") { Source = sl }); 
나는이 메시지를 얻을 : 다음 SliderValue 재산 10 '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

동적으로 MainViewModel 당신이 콜렉션이이 MVVM 스타일쪽으로 이동지지 않습니다

<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> 

에 바인드 ItemsControl를 사용할 수있는 라디오 버튼을 추가하고 각 업그레이드는 UpgradeViewModel입니다.

관련 문제