如何使用MVVM将DataGrid中的单选按钮IsChecked属性与ViewModel中存在的另一个bool属性绑定? [英] How to bind Radio Button IsChecked Property in DataGrid with another bool property existing in ViewModel using MVVM?

查看:37
本文介绍了如何使用MVVM将DataGrid中的单选按钮IsChecked属性与ViewModel中存在的另一个bool属性绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在Datagrid的RowDetailTemplate内有RadioButtons,我试图将其IsChecked属性与ViewModel中存在的另一个bool属性进行绑定.

I have RadioButtons inside a RowDetailTemplate of Datagrid and I am trying to bind it's IsChecked Property with another bool property existing in ViewModel.

我的代码是:

<DataGrid Grid.Row="0" AutoGenerateColumns="False" HorizontalAlignment="Left" CanUserAddRows="False" Name="dataGrid1" VerticalAlignment="Top" ItemsSource="{Binding Path=Messages, UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=True}">  
   <DataGrid.RowDetailsTemplate>
    <DataTemplate >
     <StackPanel Orientation="Vertical">
      <TextBlock Text="Message:" Margin="4,2,2,0"/>
      <TextBox MinHeight="120" Margin="6" MaxHeight="120" MinWidth="800" MaxWidth="800" Text="{Binding MessageBody ,Mode=OneWay}" IsReadOnly="True" HorizontalScrollBarVisibility="Auto"
         VerticalScrollBarVisibility="Auto"/>
      <StackPanel>
       <RadioButton Content="RadioButton" Height="16" Name="radioButton1" IsChecked="{Binding Path=IsMessageString, RelativeSource={RelativeSource AncestorType=DataGridRowDetailsVisibilityMode}}" />
       <RadioButton Content="RadioButton" Height="16" Name="radioButton2" IsChecked="{Binding Path= ISMessageBytes}" />
       <RadioButton Content="RadioButton" Height="16" Name="radioButton3" />
      </StackPanel>
      
     </StackPanel>
    </DataTemplate>
   </DataGrid.RowDetailsTemplate>
</DataGrid>

 

推荐答案

Hi Parag Sharma,

Hi Parag Sharma,

单选按钮通常绑定到视图模型中的相同属性.您必须将ConverterParameter与绑定一起使用.您可以在此处找到有关绑定单选按钮的信息:
http://shujaatsiddiqi.blogspot.com/2010/11/wpf-binding-radio-buttons.html

Radio buttons are generally bound to the same property in the view model. You have to use ConverterParameter with binding. You can find here about binding radio buttons:
http://shujaatsiddiqi.blogspot.com/2010/11/wpf-binding-radio-buttons.html

以上链接未在网格中进行绑定,但概念应相似.

The above link does not do binding in the grid but the concept should be similar.

如果要绑定到视图模型中的其他属性,则改用CheckBox更为合理.

If you want to bind to different properties in the view model then it would make more sense to use CheckBox instead.

谢谢,
穆罕默德

Thanks,
Muhammad


这篇关于如何使用MVVM将DataGrid中的单选按钮IsChecked属性与ViewModel中存在的另一个bool属性绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆