【wpf踩坑】RadioButton 绑定踩坑

前台代码如下:

<ItemsControl Grid.Row="1" ItemsSource="{Binding saveInfo.labConfigs , Source={x:Static md:GlobalData.Instance}}" >
            <!--<ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <UniformGrid Rows="2"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>-->
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" Margin="2" >
                        <RadioButton GroupName="a"  IsChecked="{Binding Select}"></RadioButton>
                        <!--<CheckBox  IsChecked="{Binding Select}" ></CheckBox>-->
                        <inputLayout:SfTextInputLayout Hint="产品类型" Margin="10,0,10,0" ContainerType="Filled" HintFloatMode="Float" >
                            <TextBox Text="{Binding Product}"></TextBox>
                        </inputLayout:SfTextInputLayout>
                        <StackPanel Orientation="Horizontal" Margin="1" >
                            <TextBlock Text="L(Min)" VerticalAlignment="Center"/>
                            <TextBox Text="{Binding LMim}" Foreground="SeaGreen"/>
                            <TextBlock Text="L(Max)" VerticalAlignment="Center"/>
                            <TextBox Text="{Binding LMax}" Foreground="SeaGreen"/>
                        </StackPanel>
                        <StackPanel Orientation="Horizontal" Margin="1" >
                            <TextBlock Text="A(Min)" VerticalAlignment="Center" />
                            <TextBox Text="{Binding AMim}" Foreground="Chocolate"/>
                            <TextBlock Text="A(Max)" VerticalAlignment="Center"/>
                            <TextBox Text="{Binding AMax}" Foreground="Chocolate"/>
                        </StackPanel>
                        <StackPanel Orientation="Horizontal" Margin="1">
                            <TextBlock Text="B(Min)" VerticalAlignment="Center"/>
                            <TextBox Text="{Binding BMim}" Foreground="SteelBlue"/>
                            <TextBlock Text="B(Max)" VerticalAlignment="Center"/>
                            <TextBox Text="{Binding BMax}" Foreground="SteelBlue"/>
                        </StackPanel>
                    </StackPanel>
                    
                    
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

RadioButton 是在一个列表控件的数据模板里,所以我  首先设置了RadioButton 的参数 GroupName 属性,确保多个RadioButton不会被同时选中。但是意外发生,有时就是会全部选中。

数据源如下:

public class LabConfig 
{
        public string Product { get; set; }
        public bool Select { get; set; }
        public int LMim { get; set; }
        public int LMax { get; set; }
        public int AMim { get; set; }
        public int AMax { get; set; }
        public int BMim { get; set; }
        public int BMax { get; set; }
}

最后发现,居然是这里问题:

labConfig = GlobalData.Instance.saveInfo.labConfigs.FirstOrDefault(e => e.Select = true);

改为:

labConfig = GlobalData.Instance.saveInfo.labConfigs.FirstOrDefault(e => e.Select == true);

== 写成  =

这里感觉容易出错啊,记录一下~~~

猜你喜欢

转载自blog.csdn.net/songhuangong123/article/details/131073369