从后面的代码更改booleanToVisibilityConverter - wpf c#

时间:2016-01-26 08:11:46

标签: c# wpf visibility code-behind

<Page.Resources>
    <ResourceDictionary>
        <BooleanToVisibilityConverter x:Key="booleanToVisibilityConverter" x:Name="testTest" />
    </ResourceDictionary>
</Page.Resources>

itemscontrol:

<Grid Grid.Row="1" Grid.ColumnSpan="2" Name="testName">
            <ScrollViewer VerticalScrollBarVisibility="Hidden" PanningMode="Both">
                <ItemsControl ItemsSource="{Binding Path=NextMeetingList}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Grid.Column="0" Grid.Row="0">
                                <Border x:Name = "myVariable" Grid.Column="0" Grid.Row="0" Margin="10" Height="30" Background="#A2C2E7" CornerRadius="5" BorderBrush="#A2C2E7">
                                    <Grid Margin="8,0,8,0" Background="#A2C2E7">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="1*"></ColumnDefinition>
                                            <ColumnDefinition Width="1*"></ColumnDefinition>
                                        </Grid.ColumnDefinitions>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="30"/>
                                        </Grid.RowDefinitions>
                                        <TextBlock Grid.Row="0" Grid.Column="0" Foreground="White" FontWeight="Bold" FontSize="15" Margin="0,4,0,0" HorizontalAlignment="Left">month</TextBlock>
                                        <TextBlock Grid.Row="0" Grid.Column="1"  Foreground="White" FontWeight="Bold" FontSize="15" Margin="0,4,0,0" HorizontalAlignment="Right">1 Meeting</TextBlock>
                                    </Grid>
                                </Border>
                                <Border Grid.Column="0" Grid.Row="0" Margin="10" Height="60" Background="GhostWhite" CornerRadius="3" BorderBrush="{Binding BorderColor}" BorderThickness="0,8,0,0">
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="118"></ColumnDefinition>
                                            <ColumnDefinition Width="*"></ColumnDefinition>
                                        </Grid.ColumnDefinitions>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="60"/>
                                        </Grid.RowDefinitions>
                                        <TextBlock Grid.Row="0" Grid.Column="0" HorizontalAlignment="Left" FontSize="40" Margin="10,5,0,0" Style="{DynamicResource Lato-Semibold}" Text="{Binding endDate.Day}"/>
                                        <TextBlock Grid.Row="0" Grid.Column="0" HorizontalAlignment="Left" FontSize="12" Margin="77,13,0,0" Height="Auto" Style="{DynamicResource Lato-Semibold}" Text="{Binding DayString}"/>
                                        <TextBlock Grid.Row="0" Grid.Column="0" HorizontalAlignment="Left" FontSize="14" Margin="70,26,0,0" Height="Auto" Style="{DynamicResource Lato-Semibold}" Text="{Binding endDate.Hour}"/>
                                        <TextBlock Grid.Row="0" Grid.Column="0" HorizontalAlignment="Left" FontSize="14" Margin="86,26,0,0" Height="Auto" Style="{DynamicResource Lato-Semibold}" Text=":"/>
                                        <TextBlock Grid.Row="0" Grid.Column="0" HorizontalAlignment="Left" FontSize="14" Margin="90,26,0,0" Height="Auto" Style="{DynamicResource Lato-Semibold}" Text="{Binding MinuteString}"/>
                                        <Border Grid.Row="0" Grid.Column="0" Width="1" BorderBrush="#BABABA" Height="40" Margin="115,-6,0,0" BorderThickness="1" HorizontalAlignment="Left"></Border>
                                        <TextBlock Grid.Row="0" Grid.Column="1" HorizontalAlignment="Left" Text="{Binding subject}" FontWeight="Bold" FontSize="17" Margin="10,10,0,0"/>
                                        <TextBlock Grid.Row="0" Grid.Column="1" Width="Auto" TextWrapping="Wrap" HorizontalAlignment="Left" Text="{Binding descr}" FontSize="10"  Margin="20,27,150,0"/>
                                        <TextBlock Grid.Row="0" Grid.Column="1" HorizontalAlignment="Right" Text="{Binding companyName}" FontSize="10" Margin="0,10,30,0"/>
                                        <TextBlock Grid.Row="0" Grid.Column="1" HorizontalAlignment="Right" Text="sala del consiglio" FontSize="10" Margin="0,27,30,0"/>
                                        <TextBlock Grid.Row="0" Grid.Column="1" HorizontalAlignment="Right" Text=">" FontSize="15" VerticalAlignment="Center" Margin="0,-5,10,0"/>
                                    </Grid>
                                </Border>
                            </StackPanel>                                    
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </ScrollViewer>
        </Grid>

各位大家好,我应该可以在WPF中的ItemsControl中复制一个网格。

问题是我无法理解如何从代码中使用booleanToVisibilityConverter。 也许我错过了一杯水,但我无法连接怎么办,如果名称TestTest隔膜无法看到它,那么代码隐藏。

从背后的代码我看不到&#34; myVariable&#34;

1 个答案:

答案 0 :(得分:3)

要从代码隐藏中获取转换器,您必须在Page的{​​{1}}字典中查找它。在Resources课程中执行以下操作:

Page

不需要为转换器的资源条目IValueConverter converter = this.Resources["booleanToVisibilityConverter"] as IValueConverter; ;在这里你想使用Name。此外,转换器通常与XAML绑定一起使用 - 如果你想在代码中使用它,你可以只实例化一个:

Key

常见用法是这样的:

IValueConverter converter = new BooleanToVisibilityConverter();

代码隐藏,使用窗口类本身作为数据上下文:

<Window.Resources>
    <ResourceDictionary>
        <BooleanToVisibilityConverter x:Key="Converter" />
    </ResourceDictionary>
</Window.Resources>
<Grid Visibility="{Binding Display, Converter={StaticResource Converter}}">
    ...
</Grid>

好的,还有一次,这次没有数据绑定或转换器:

public partial class MainWindow
{
    public bool Display { get; set; };

    public MainWindow()
    {
        InitializeComponent();
        DataContext = this; // For the binding
        Display = true;
    }
}

代码隐藏:

<Grid x:Name="myGrid">
    ...
</Grid>

以上并没有解释为什么OP无法从代码隐藏引用“myVariable”。那是因为它在一个模板中,每个项目中的一个项目(动态)实例化一次。

仍然可以从代码中动态地获取它。例如,此XAML使用public partial class MainWindow { public MainWindow() { InitializeComponent(); myGrid.Visibility = Visibility.Visible; } }

DataTemplate

以下代码隐藏让我们进入模板并使“innerGrid”有选择地消失:

<ItemsControl ItemsSource="{Binding Names}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid x:Name="innerGrid" Loaded="OnGridLoaded" DataContext="{Binding Name, Mode=OneWay}">
                    <TextBox Text="{Binding Path=., Mode=OneWay}" />
                </Grid>
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

(为了使字符串易于访问,我将public class Data { public string Name { get; } public Data(string name) { Name = name; } } public partial class MainWindow { public Data[] Names { get; } = { new Data("Hello"), new Data("World") }; public MainWindow() { InitializeComponent(); DataContext = this; } private void OnGridLoaded(object sender, RoutedEventArgs e) { Grid outerGrid = sender as Grid; Grid innerGrid = outerGrid?.FindName("innerGrid") as Grid; if (innerGrid != null) { string name = innerGrid.DataContext as string; if (name == "Hello") { innerGrid.Visibility = Visibility.Collapsed; } } } } 对象绑定到“innerGrid”Data。)