2017-08-07 20 views
0
<ListBox ItemsSource="{Binding XyzList}" BorderThickness="0" Background="Transparent"> 
    <ListBox.ItemContainerStyle> 
     <Style TargetType="{x:Type ListBoxItem}"> 
      <Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}" /> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="ListBoxItem"> 
         <ContentPresenter/> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </ListBox.ItemContainerStyle> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <Grid> 
       <Image Visibility="{Binding Stop}" 
         ToolTip="{Binding Stopp}" 
         HorizontalAlignment="Left" 
         Opacity="1" Width="11" Height="11" 
         Source=stop.png"/> 
       <RadioButton Content="{Binding Period}" 
          IsEnabled="{Binding Ok}" 
          IsChecked="{Binding IsSelected}" 
          Margin="20,0,0,0" HorizontalAlignment="Left"         
          Command="Views:ValjLeveransArende.PrCommand"/> 
      </Grid> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

當我第一次選擇單選按鈕時,它不會被選中,我會得到正確的數據,但不會被選中。列表框中的wpf單選按鈕不會第一次檢查

當我第二次嘗試時,它被選中。

的更多信息:

public class XyzList: WorkViewModelBase 
{ 
    private bool _isSelected; 
    public bool IsSelected 
    { 
     get { return _isSelected; } 
     set 
     { 
      _isSelected = value; 
      OnPropertyChanged(nameof(IsSelected)); 
     } 
    } 
} 

,然後當我填寫我將它設置這樣

list.Add(new XyzList() 
      {      
       IsSelected = false 
      }); 
+0

程序啓動時'IsSelected'的值是多少?這聽起來像一個綁定的問題... –

+0

它被設置爲false在代碼 –

+0

您是否嘗試過更改您的xamlto ...複選框IsChecked =「{綁定IsSelected,UpdateSourceTrigger = PropertyChanged}」? – DRapp

回答

2

數據的其餘部分看起來你綁定兩個IsSelected地方,它們都被觸發相同的用戶操作。相反,您應該只保留一個View-to-Viewmodel綁定,並將不同的視圖控件鏈接到另一個。

保持在ItemContainerStyle

<Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}" /> 

變化的RadioButton

IsChecked="{Binding IsSelected,RelativeSource={RelativeSource AncestorType=ListBoxItem}}" 

另一個問題:你的單選按鈕將不會像它們是否屬於同一個組......你可能如果您確實需要其功能,則必須與RadioButton.GroupName屬性配合使用。但是,完全可以依靠ListBox進行選擇。

+0

我試了一下,但還是沒有選擇第一次=(( –

+0

@KickanG)你有什麼在命令或屬性設置代碼可能會干擾功能? – grek40

+0

在我的文章中添加了一些信息 –

相關問題