使用可观察收集和数据模板的列表框中的C#WPF单选按钮
我正在使用与数据绑定Demo()类似的结构制作一个程序 我现在正在尝试在列表框数据模板中实现单选按钮。我的目标是修改适当问题对象的“选定”值。我想这是可能的,通过给我的模板中2个RadioButton元素的“Checked”属性提供适当的绑定,但到目前为止我还不能做到这一点。我的另一个尝试是在代码隐藏中实现这一点,但我在这里也失败了。谢谢你的建议 问题类使用可观察收集和数据模板的列表框中的C#WPF单选按钮,c#,wpf,templates,listbox,radio-button,C#,Wpf,Templates,Listbox,Radio Button,我正在使用与数据绑定Demo()类似的结构制作一个程序 我现在正在尝试在列表框数据模板中实现单选按钮。我的目标是修改适当问题对象的“选定”值。我想这是可能的,通过给我的模板中2个RadioButton元素的“Checked”属性提供适当的绑定,但到目前为止我还不能做到这一点。我的另一个尝试是在代码隐藏中实现这一点,但我在这里也失败了。谢谢你的建议 问题类 xaml文件 <Window x:Class="WpfApplication1.Audyt_window" xmln
xaml文件
<Window x:Class="WpfApplication1.Audyt_window"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Audyt"
xmlns:src="clr-namespace:WpfApplication1"
ResizeMode="NoResize"
SizeToContent="WidthAndHeight"
WindowStartupLocation="CenterScreen"
>
<Window.Resources>
<CollectionViewSource Source="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Window}, Path=Questions}" x:Key="questions_collection" />
<Style x:Key="text_style" TargetType="TextBlock">
<Setter Property="Foreground" Value="#333333" />
</Style>
<DataTemplate x:Key="Questions_Template" DataType="{x:Type src:Question}">
<Border BorderThickness="2" BorderBrush="Brown" Padding="7" Name="Question_List_Border" Margin="3" Width="365">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20"/>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="60"/>
<ColumnDefinition Width="60"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0"
Name="textblock_ID"
Style="{StaticResource text_style}"
Text="ID: "
>
</TextBlock>
<TextBlock Grid.Row="0" Grid.Column="1"
Name="textblock2_ID"
Style="{StaticResource text_style}"
Text="{Binding Path=_ID}"
>
</TextBlock>
<TextBlock Grid.Row="1" Grid.Column="0"
Name="textblock_question_number"
Style="{StaticResource text_style}"
Text="Val"
>
</TextBlock>
<TextBlock Grid.Row="1" Grid.Column="1"
Name="textblock2_question_number"
Style="{StaticResource text_style}"
Text="{Binding Path=_question_number}"
>
</TextBlock>
<TextBlock Grid.Row="0" Grid.Column="2" Grid.RowSpan="3" TextWrapping="Wrap"
Name="textblock_question"
Style="{StaticResource text_style}"
Text="{Binding Path=_question}"
>
</TextBlock>
<RadioButton GroupName="{Binding Path=_ID}" Grid.Row="0" Grid.Column="3" HorizontalAlignment="Center" VerticalAlignment="Center" Checked="RadioButtons_Yes" Margin="10,17,11,17">Tak</RadioButton>
<RadioButton GroupName="{Binding Path=_ID}" Grid.Row="0" Grid.Column="4" HorizontalAlignment="Center" VerticalAlignment="Center" Checked="RadioButtons_No" Margin="10,17,11,17">Nie</RadioButton>
</Grid>
</Border>
</DataTemplate>
</Window.Resources>
<Border Padding="10">
<Grid>
<ListBox Name="Questions_View_List" HorizontalAlignment="Left" VerticalAlignment="Top"
Height="525" Width="400" Margin="0,0,0,0" BorderThickness="2" BorderBrush="DimGray"
ItemsSource="{Binding Source={StaticResource questions_collection}}"
ItemTemplate="{StaticResource Questions_Template}"
SelectionMode="Single"
SelectedValue="{Binding Path=_ID}"
SelectedValuePath="{Binding Path=_ID}"
SelectionChanged="Questions_View_List_SelectionChanged"
>
</ListBox>
</Grid>
</Border>
</Window>
德
聂
代码隐藏
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.ComponentModel;
namespace WpfApplication1
{
public partial class Audyt_window : Window
{
CollectionViewSource questions_collection;
private ObservableCollection<Question> questions = new ObservableCollection<Question>();
public ObservableCollection<Question> Questions
{
get { return this.questions; }
set { this.questions = value; }
}
public Audyt_window()
{
DataContext = this;
load_temp_data();
InitializeComponent();
questions_collection = (CollectionViewSource)(this.Resources["questions_collection"]);
}
private void load_temp_data()
{
Question que1 = new Question(1, 2, "Question1", false, false);
Question que2 = new Question(2, 1, "Question2", false, false);
Question que3 = new Question(3, 0, "Question3", false, false);
this.Questions.Add(que1);
this.Questions.Add(que2);
this.Questions.Add(que3);
}
private void Questions_View_List_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Question question_temp = (Question)((ListBox)e.Source).SelectedValue;
}
private void RadioButtons_Yes(object sender, RoutedEventArgs e)
{
//Question question_temp = (Question)((ListBox)e.Source(RadioButton)e.Source; // Can't really figure this out ;/
//Variables.selected_question = question_temp._ID;
}
private void RadioButtons_No(object sender, RoutedEventArgs e)
{
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Shapes;
使用系统数据;
使用System.Data.SqlClient;
使用系统诊断;
使用系统组件模型;
命名空间WpfApplication1
{
公共部分类Audyt\u窗口:窗口
{
集合查看源问题集合;
私有ObservableCollection问题=新ObservableCollection();
公开收集问题
{
获取{返回this.questions;}
设置{this.questions=value;}
}
公共音频窗口()
{
DataContext=this;
加载温度数据();
初始化组件();
questions_collection=(CollectionViewSource)(this.Resources[“questions_collection]”);
}
专用无效加载温度数据()
{
问题que1=新问题(1,2,“问题1”,假,假);
问题que2=新问题(2,1,“问题2”,假,假);
问题que3=新问题(3,0,“问题3”,假,假);
本.问题.添加(问题1);
本.问题.添加(问题2);
本.问题.添加(问题3);
}
私人无效问题\u查看\u列表\u选择已更改(对象发送者,选择更改发送者)
{
//问题温度=(问题)((列表框)e.Source)。SelectedValue;
}
专用无效单选按钮\u是(对象发送器、路由目标e)
{
//问题温度=(问题)((列表框)e.Source(单选按钮)e.Source;//真的搞不懂;/
//Variables.selected\u question=问题\u temp.\u ID;
}
专用无效单选按钮\u编号(对象发送器,路由目标e)
{
}
}
}
虽然你的问题仍然有点不清楚,因为与你的百万行代码相比,你只使用了几行单词,但我相信我知道你在追求什么。我认为你需要创建一个枚举
来区分你问题的不同答案……也许就这么简单?:
public enum Answer { A, B, C, D ... }
现在您有了一个枚举
,您可以在不同的单选按钮
上使用它,以及枚举工具转换器
:
<StackPanel>
<RadioButton IsChecked="{Binding AnswerProperty, Converter={StaticResource
EnumToBoolConverter}, ConverterParameter=A}" Content="Answer A" />
<RadioButton IsChecked="{Binding AnswerProperty, Converter={StaticResource
EnumToBoolConverter}, ConverterParameter=B}" Content="Answer B" />
<RadioButton IsChecked="{Binding AnswerProperty, Converter={StaticResource
EnumToBoolConverter}, ConverterParameter=C}" Content="Answer C" />
<RadioButton IsChecked="{Binding AnswerProperty, Converter={StaticResource
EnumToBoolConverter}, ConverterParameter=D}" Content="Answer D" />
</StackPanel>
此处的
AnswerProperty
与您所称的适当问题对象的“选定”值相关。当然,您现在需要将该属性类型更改为enum
的类型。您可以在线找到许多不同版本的enumtoolconverter
,问题h的答案中有一个ere在堆栈溢出时出错。尝试此操作,因为您想在选择收音机时更改问题对象
<RadioButton Command="{Binding RadioCommand}" CommandParameter="Tak" GroupName="MyRadioGroup">Tak</RadioButton>
<RadioButton Command="{Binding RadioCommand}" CommandParameter="Nie" GroupName="MyRadioGroup">Nie</RadioButton>
}
Mycommand或您可以使用其他命令,如Relaycommand
公共类MyCommand:ICommand
{
行动执行;
Func canExecute;
公共MyCommand(操作执行、功能执行)
{
this.executeAction=executeAction;
this.canExecute=canExecute;
}
公共布尔CanExecute(对象参数)
{
如果(canExecute!=null)
return canExecute();
其他的
返回true;
}
public void raisecancecutechanged()
{
如果(CanExecuteChanged!=null)
CanExecuteChanged(这是新的EventArgs());
}
公共事件处理程序CanExecuteChanged;
public void Execute(对象参数)
{
if(executeAction!=null)
执行(参数);
}
}
如果第一个单选按钮负责将_selected设置为yes/no,然后在其中设置为checked=“{Binding _selected,Mode=TwoWay}”,则两个单选按钮都分配给相同的组名,因此您只能同时选择其中一个。谢谢。这实际上是最简单但有效的解决方案。
<RadioButton Command="{Binding RadioCommand}" CommandParameter="Tak" GroupName="MyRadioGroup">Tak</RadioButton>
<RadioButton Command="{Binding RadioCommand}" CommandParameter="Nie" GroupName="MyRadioGroup">Nie</RadioButton>
public class Question : INotifyPropertyChanged
{
//your existing code
//....
//.....
//Add Command to your Question class as below
MyCommand radioCommand;
public MyCommand RadioCommand
{
get { return radioCommand ?? (radioCommand = new MyCommand(OnRadioCommand, () => true)); }
}
void OnRadioCommand(object obj)
{
if (obj != null)
{
var checkedRadiochecked = obj.ToString();
//checkedRadiochecked will have either Tak or Nie according to
// RadioButton checked
//do your stuff of changing this object here
}
}
}
public class MyCommand : ICommand
{
Action<object> executeAction;
Func<bool> canExecute;
public MyCommand(Action<object> executeAction, Func<bool> canExecute)
{
this.executeAction = executeAction;
this.canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
if (canExecute != null)
return canExecute();
else
return true;
}
public void RaiseCanExecuteChanged()
{
if (CanExecuteChanged != null)
CanExecuteChanged(this, new EventArgs());
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
if (executeAction != null)
executeAction(parameter);
}
}