将布尔值更改为可见性转换器从代码隐藏 - wpf c #

本文关键字:隐藏 代码 wpf 转换器 布尔值 可见性 | 更新日期: 2023-09-27 18:32:04

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

项目控制:

<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中折叠网格。

问题是我无法理解如何从代码隐藏中使用布尔到可见性转换器。也许我错过了一杯水,但我无法连接该怎么做,如果名称TestTest隔膜看不到它,那么代码隐藏。

从代码隐藏中,我没有看到"myVariable"

将布尔值更改为可见性转换器从代码隐藏 - wpf c #

要从代码隐藏中获取转换器,您必须在PageResources字典中查找它。在Page课上做这样的事情:

IValueConverter converter = this.Resources["booleanToVisibilityConverter"] as IValueConverter;

没有必要为转换器的资源条目提供一个Name;在这里你想使用Key。此外,转换器通常与 XAML 绑定一起使用 - 如果需要代码中的转换器,只需实例化一个:

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>

代码隐藏:

public partial class MainWindow
{
    public MainWindow()
    {
        InitializeComponent();
        myGrid.Visibility = Visibility.Visible;
    }
}

上面没有解释为什么OP不能从代码隐藏中引用"myVariable"。这是因为它位于模板中,该模板(动态)实例化一次集合中的每个项。

仍然可以从代码中动态地获取它。例如,此 XAML 使用DataTemplate

<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>

下面的代码隐藏让我们进入模板,有选择地让"innerGrid"消失:

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;
            }
        }
    }
}

(为了使字符串易于访问,我将Data对象绑定到"innerGrid"DataContext