简体   繁体   English

从后面的代码更改booleanToVisibilityConverter-WPF C#

[英]change booleanToVisibilityConverter from code behind - wpf c #

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

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

Hello everyone , I should be able to make a grid Collapsed inside a ItemsControl in WPF . 大家好,我应该能够在WPF的ItemsControl中创建一个已折叠的网格。

The problem is that I can not understand how to use the booleanToVisibilityConverter from code behind . 问题是我无法从后面的代码中了解如何使用booleanToVisibilityConverter。 Maybe I'm missing out on a glass of water but I can not connect how to do, if the name TestTest septum can not see it then the code-behind . 也许我错过了一杯水,但是我无法连接该怎么办,如果名称TestTest septum看不到它,那么代码将隐藏在后面。

from code behind i don't see "myVariable" 从后面的代码中我看不到“ myVariable”

To get hold of the converter from code-behind, you'd have to look it up in the Page 's Resources dictionary. 要从后台获取转换器,您必须在PageResources字典中查找它。 Do something like this in your Page class: 在您的Page类中执行以下操作:

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

Giving the converter's resource entry a Name is not necessary; 不必为转换器的资源条目Name here you want to use the Key . 在这里您要使用Key Also, converters are normally used with XAML bindings -- if you want one in code, you can just instantiate one: 此外,转换器通常与XAML绑定一起使用-如果您希望在代码中使用一个,则只需实例化一个即可:

IValueConverter converter = new BooleanToVisibilityConverter();

Common usage goes something like this: 常见用法如下:

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

Code-behind, using the window class itself as the data context: 在代码隐藏中,使用window类本身作为数据上下文:

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

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

Okay, one more, this time without data binding or converter: 好的,又一次,这次没有数据绑定或转换器:

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

Code-behind: 后台代码:

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

The above doesn't explain why the OP couldn't reference "myVariable" from code-behind. 上面没有解释为什么OP无法从代码后面引用“ myVariable”。 That's because it's in a template, which is instantiated (dynamically) once per item in the collection. 这是因为它在模板中,该模板在集合中的每个项目均被实例化(动态)一次。

It's still possible to get at it from code -- dynamically. 仍然可以从代码中动态地获取它。 For example, this XAML uses a DataTemplate : 例如,此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>

The following code-behind lets us reach into the template and make "innerGrid" disappear selectively: 下面的代码让我们进入模板,使“ 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;
            }
        }
    }
}

(To make the string easily accessible I bound the Data object to the "innerGrid" DataContext .) (为了使字符串易于访问,我将Data对象绑定到了“ innerGrid” DataContext 。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM