2017-08-14 6 views
-2

私はWPFでのデータバインディングの作業に慣れていません。私のコードを見てください。私は1つだけの選択をするようにユーザーを制限したい。WPFでチェックボックスを1つだけチェックする方法は?

より一般的な質問は、私がhandleCheckedメソッドで行ったように、データソースをループするのを避ける方法は?私はItemSourceをループする必要はないと思っていますが、方法はわかりません。

XAML:背後に

<Window x:Class="TheProgram.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="News Chooser"> 
<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="9*" /> 
     <RowDefinition Height="1*" /> 
    </Grid.RowDefinitions> 
    <Grid Name="newsChooser" Grid.Row="0" Margin="10"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="8*" /> 
     </Grid.RowDefinitions> 
     <ComboBox Name="newsCategory" Margin="10,8,10,5" Grid.Row="0"/> 
     <ComboBox Name="newsSrc" SelectionChanged="handleNewsSrcChange" Margin="10,5,10,8" Grid.Row="1"/> 
     <DataGrid SelectionMode="Single" IsReadOnly="True" Margin="10,0,10,8" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="2" Name="newsStories" AutoGenerateColumns="False" RowHeaderWidth="0"> 
      <DataGrid.Columns> 
       <DataGridTextColumn Width="8*" Header="Headline" Binding="{Binding Path=heading}"> 
        <DataGridTextColumn.HeaderStyle> 
         <Style TargetType="DataGridColumnHeader"> 
          <Setter Property="HorizontalContentAlignment" Value="Center" /> 
         </Style> 
        </DataGridTextColumn.HeaderStyle> 
        <DataGridTextColumn.ElementStyle> 
         <Style> 
          <Setter Property="TextBlock.TextWrapping" Value="Wrap" /> 
          <Setter Property="TextBlock.Padding" Value="5" /> 
         </Style> 
        </DataGridTextColumn.ElementStyle> 
       </DataGridTextColumn> 
       <DataGridCheckBoxColumn Header="Select" Width="2*" Binding="{Binding Path=isIncluded, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}"> 
        <DataGridCheckBoxColumn.HeaderStyle> 
         <Style TargetType="DataGridColumnHeader"> 
          <Setter Property="HorizontalContentAlignment" Value="Center" /> 
         </Style> 
        </DataGridCheckBoxColumn.HeaderStyle> 
        <DataGridCheckBoxColumn.ElementStyle> 
         <Style> 
          <Setter Property="TextBlock.HorizontalAlignment" Value="Center" /> 
          <Setter Property="TextBlock.VerticalAlignment" Value="Center" /> 
         </Style> 
        </DataGridCheckBoxColumn.ElementStyle> 
        <DataGridCheckBoxColumn.CellStyle> 
         <Style> 
          <EventSetter Event="CheckBox.Checked" Handler="handleChecked" /> 
          <EventSetter Event="CheckBox.Unchecked" Handler="handleChecked" /> 
         </Style> 
        </DataGridCheckBoxColumn.CellStyle> 
       </DataGridCheckBoxColumn> 
      </DataGrid.Columns> 
     </DataGrid> 
    </Grid> 
    <StackPanel Grid.Row="1" Grid.ColumnSpan="2" Orientation="Horizontal" Margin="20,0,10,0"> 
     <Button Click="handleSendButton" Height="30" Width="70">Send</Button> 
    </StackPanel> 
</Grid> 

コード:

namespace TheProgram 
{ 

class NewsSrcTableItm 
{ 
    public string heading { get; set; } 
    public NewsItem itm { get; set; } 
    public bool isIncluded { get; set; } 
} 

public partial class MainWindow : Window 
{ 

    private List<NewsItem> includedList = null; 

    public MainWindow() 
    { 
     InitializeComponent(); 
     setNewsSourceComboBox(); 

     this.includedList = new List<NewsItem>(); 

     initializeNewsTable(); 
    } 

    private void setNewsSourceComboBox() 
    { 
     ComboBox cbx = (ComboBox)this.FindName("newsSrc"); 
     cbx.ItemsSource = MainWinClass.comboBoxList(); 
     cbx.SelectedIndex = 0; 

     ComboBox cbx1 = (ComboBox)this.FindName("newsCategory"); 
     cbx1.ItemsSource = MainWinClass.categoryList(); 
     cbx1.SelectedIndex = 0; 
    } 

    private void initializeNewsTable() 
    { 
     newsStories.ItemsSource = MainWinClass.initializeNewsSource(); 
    } 


    public void handleChecked(object sender, RoutedEventArgs e) 
    { 
     ComboBox cbx = this.FindName("newsCategory") as ComboBox; 
     String theSelected = Regex.Replace(cbx.SelectedValue.ToString(), " ", String.Empty); 
     String theNewsCategory = char.ToLower(theSelected[0]) + theSelected.Substring(1); 
     foreach (var r in newsStories.ItemsSource) 
     { 
      NewsSrcTableItm itm = (NewsSrcTableItm)r; 
      if (itm.isIncluded) 
      { 
       if (itm.itm.getCategory() == null) 
       { 
        itm.itm.setCategory(theNewsCategory); 
       } 
       this.includedList.Add(itm.itm); 
      } 
     } 
    } 

    public void handleNewsSrcChange(object Sender, EventArgs args) 
    { 
     ComboBox cb = this.FindName("newsSrc") as ComboBox; 
     newsStories.ItemsSource = MainWinClass.changeSource(cb.SelectedValue.ToString()); 
    } 

    public async void handleSendButton(object Sender, EventArgs args) 
    { 
     NewsItem itm = this.includedList[0]; 
     String category = itm.getCategory(); 
     String title = itm.getHeading(); 
     String extract = null; 
     String content = null; 
     String timestamp = DateTime.Now.ToString("yyyyMMdd"); 
     NewsContentFetcher f = new NewsContentFetcher(itm.getUrl()); 
     List<String> theList = await f.getContent(); 
     extract = theList[0]; 
     foreach (String s in theList) 
     { 
      content += s; 
     } 
     HttpClient client = new HttpClient(); 
     var values = new Dictionary<String, String> 
     { 
      { "category", category }, 
      { "title", title }, 
      { "extract", extract }, 
      { "content", content }, 
      { "timestamp", timestamp } 
     }; 
     var theContent = new FormUrlEncodedContent(values); 
     var result = client.PostAsync("http://localhost/addNews.php", theContent).Result; 
     int statusCode = (int)result.StatusCode; 
     if (statusCode == 200) 
     { 
      MessageBox.Show("Completed"); 
     } 
     else 
     { 
      MessageBox.Show("failed"); 
     } 
    } 

} 
} 
+0

。 – mm8

+0

@ mm8お返事ありがとうございます。しかし、チェックされたチェックボックスのインデックスを直接取得して、インデックスにバインドされたデータを変更できるようにしていますか? –

+0

handleCheckedイベントハンドラの意味ですか?私の答えを見てください。 – mm8

答えて

0

しかし、私は与えられたバインドさのデータを修正することができるように直接確認し、チェックボックスのインデックスを取得するためにとにかくありインデックス?

これを試してみてください:

あなたはアイテム一つの方法か、確認したい場合は、別のは/それらをオフを反復処理する必要があります。もちろん、
public void handleChecked(object sender, RoutedEventArgs e) 
{ 
    CheckBox chk = e.OriginalSender as CheckBox; 
    NewsSrcTableItm itm = chk.DataContext as NewsSrcTableItm; 

    //get index: 
    var sourceCollection = newsStories.ItemsSource as IList<NewsSrcTableItm>; 
    int index = sourceCollection.IndexOf(itm); 
    //... 
} 
+0

は機能しません。 NewsSrcTableItemのNullPointerException = NewsSrcTableItmというchk.DataContext。 –

+0

chkまたはitemはnullですか? – mm8

+0

chi is null ..... –

関連する問題