none
绑定CheckBox的 IsChecked属性的问题 RRS feed

答案

  •  你好 ,

    你可以这样理解:DataGrid中的列是个抽象的对象,它不会出现在VisualTree上面, 所以你用绑定的时候,根据ElementName, 它是找不到东西的,所以绑定是不工作的。

    另外你说的起作用的是使用的DataGridTextColumn.Binding 属性,虽然这个属性叫做Binding,但它并不是一个绑定,可以说是一个赋值操作。

    另外,如果你希望 绑定可以在DataGridTextColumn工作,可以工作使用Source 和 x:Reference 作为一个workaround。即:

    <Window x:Class="Sample.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local ="clr-namespace:Sample"
            Title="MainWindow" Height="350" Width="525" SizeToContent="Width">
        <Window.Resources>
            <local:MyCheckBoxConverter x:Key="MyCheckBoxConverter"></local:MyCheckBoxConverter>
            <local:MyComboBoxConverter x:Key="MyComboBoxConverter"></local:MyComboBoxConverter>
            <local:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"></local:BooleanToVisibilityConverter>
          
            
            <DataGridTextColumn x:Key="mytest" Header="ColumnA" Binding="{Binding TextStr}" 
                                Visibility="{Binding Path=IsChecked,Source={x:Reference cbShowColumnA},
                        Converter={StaticResource BooleanToVisibilityConverter}}">
            </DataGridTextColumn>
        </Window.Resources>
            <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="auto"></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <StackPanel Margin="5" Orientation="Horizontal">
                <CheckBox Name="cbUpper" Content="大写" Margin="20,0,20,0"  VerticalAlignment="Center"></CheckBox>
                <CheckBox Name="cbShowColumnA" Tag="ABCDEF" Content="显示ColumnA" IsChecked="True"  Margin="20,0,20,0"  VerticalAlignment="Center"></CheckBox>
                <ComboBox Name="cmbList"></ComboBox>
            </StackPanel>
            <DataGrid Name="dgShowList" Grid.Row="1" AutoGenerateColumns="False">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="原字符串" Binding="{Binding TextStr}"></DataGridTextColumn>
                    <DataGridTextColumn Header="字符串长度" Binding="{Binding Lenth}"></DataGridTextColumn>
                    <DataGridTextColumn Header="CheckBox绑定修改符串">
                        <DataGridTextColumn.Binding>
                            <MultiBinding Converter="{StaticResource MyCheckBoxConverter}">
                                <Binding Path="IsChecked" ElementName="cbUpper"></Binding>
                                <Binding Path="TextStr"></Binding>
                            </MultiBinding>
                        </DataGridTextColumn.Binding>
                    </DataGridTextColumn>
                    <DataGridTextColumn Header="ComboBox绑定修改符串">
                        <DataGridTextColumn.Binding>
                            <MultiBinding Converter="{StaticResource MyComboBoxConverter}">
                                <Binding Path="SelectedItem" ElementName="cmbList"></Binding>
                                <Binding Path="TextStr"></Binding>
                            </MultiBinding>
                        </DataGridTextColumn.Binding>
                    </DataGridTextColumn>
                    <!--<DataGridTextColumn Header="ColumnA" Binding="{Binding TextStr}" 
                                        Visibility="{Binding Path=IsChecked, ElementName=cbShowColumnA,
                        Converter={StaticResource test}}">
                    </DataGridTextColumn>
                    <StaticResource ResourceKey="BooleanToVisibilityConverter"/>-->
                </DataGrid.Columns>
            </DataGrid>
        </Grid>
    </Window>
    

    你可以参考这个帖子:http://social.msdn.microsoft.com/Forums/vstudio/en-US/a1384793-436b-45fc-9973-cec9247c2a86/wpf-datagridtextcolumn-visibility-converter


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • 已标记为答案 blwuer 2013年7月8日 7:47
    2013年7月8日 7:26
    版主

全部回复

  • 怎么没人回复呢?
    2013年7月8日 3:00
  •  你好 ,

    你可以这样理解:DataGrid中的列是个抽象的对象,它不会出现在VisualTree上面, 所以你用绑定的时候,根据ElementName, 它是找不到东西的,所以绑定是不工作的。

    另外你说的起作用的是使用的DataGridTextColumn.Binding 属性,虽然这个属性叫做Binding,但它并不是一个绑定,可以说是一个赋值操作。

    另外,如果你希望 绑定可以在DataGridTextColumn工作,可以工作使用Source 和 x:Reference 作为一个workaround。即:

    <Window x:Class="Sample.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local ="clr-namespace:Sample"
            Title="MainWindow" Height="350" Width="525" SizeToContent="Width">
        <Window.Resources>
            <local:MyCheckBoxConverter x:Key="MyCheckBoxConverter"></local:MyCheckBoxConverter>
            <local:MyComboBoxConverter x:Key="MyComboBoxConverter"></local:MyComboBoxConverter>
            <local:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"></local:BooleanToVisibilityConverter>
          
            
            <DataGridTextColumn x:Key="mytest" Header="ColumnA" Binding="{Binding TextStr}" 
                                Visibility="{Binding Path=IsChecked,Source={x:Reference cbShowColumnA},
                        Converter={StaticResource BooleanToVisibilityConverter}}">
            </DataGridTextColumn>
        </Window.Resources>
            <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="auto"></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <StackPanel Margin="5" Orientation="Horizontal">
                <CheckBox Name="cbUpper" Content="大写" Margin="20,0,20,0"  VerticalAlignment="Center"></CheckBox>
                <CheckBox Name="cbShowColumnA" Tag="ABCDEF" Content="显示ColumnA" IsChecked="True"  Margin="20,0,20,0"  VerticalAlignment="Center"></CheckBox>
                <ComboBox Name="cmbList"></ComboBox>
            </StackPanel>
            <DataGrid Name="dgShowList" Grid.Row="1" AutoGenerateColumns="False">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="原字符串" Binding="{Binding TextStr}"></DataGridTextColumn>
                    <DataGridTextColumn Header="字符串长度" Binding="{Binding Lenth}"></DataGridTextColumn>
                    <DataGridTextColumn Header="CheckBox绑定修改符串">
                        <DataGridTextColumn.Binding>
                            <MultiBinding Converter="{StaticResource MyCheckBoxConverter}">
                                <Binding Path="IsChecked" ElementName="cbUpper"></Binding>
                                <Binding Path="TextStr"></Binding>
                            </MultiBinding>
                        </DataGridTextColumn.Binding>
                    </DataGridTextColumn>
                    <DataGridTextColumn Header="ComboBox绑定修改符串">
                        <DataGridTextColumn.Binding>
                            <MultiBinding Converter="{StaticResource MyComboBoxConverter}">
                                <Binding Path="SelectedItem" ElementName="cmbList"></Binding>
                                <Binding Path="TextStr"></Binding>
                            </MultiBinding>
                        </DataGridTextColumn.Binding>
                    </DataGridTextColumn>
                    <!--<DataGridTextColumn Header="ColumnA" Binding="{Binding TextStr}" 
                                        Visibility="{Binding Path=IsChecked, ElementName=cbShowColumnA,
                        Converter={StaticResource test}}">
                    </DataGridTextColumn>
                    <StaticResource ResourceKey="BooleanToVisibilityConverter"/>-->
                </DataGrid.Columns>
            </DataGrid>
        </Grid>
    </Window>
    

    你可以参考这个帖子:http://social.msdn.microsoft.com/Forums/vstudio/en-US/a1384793-436b-45fc-9973-cec9247c2a86/wpf-datagridtextcolumn-visibility-converter


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • 已标记为答案 blwuer 2013年7月8日 7:47
    2013年7月8日 7:26
    版主