gpt4 book ai didi

c# - 如何更改 gridview 中组合框的可见性

转载 作者:行者123 更新时间:2023-11-30 18:12:57 24 4
gpt4 key购买 nike

我想更改组合框在 GridView 中的可见性。当值为空或 null 时,需要隐藏组合框。到目前为止,这是我的代码。 gvCursisten 是 GridView 。

编辑:这在 xaml 中有效,但在 cs 中无效。

  <ListView Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2" Grid.RowSpan="2" Name="lvTest">
<ListView.View>
<GridView x:Name="gvCursisten">
<GridViewColumn Header="Property" Width="160">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="PropertyComboBox" ItemsSource="{Binding Path=Voornaam}" Width="140" >
<ComboBox.Style>
<Style TargetType="ComboBox">
<Setter Property="Visibility" Value="Visible"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=txtTest, Path=Text}" Value="Hoi">
<Setter Property="Visibility" Value="Hidden"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
</ComboBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>

C# code
GridViewColumn gvcCombobox2 = new GridViewColumn();
gvcCombobox2.Header = "Combobox2";
gvcCombobox2.Width = 200;

DataTemplate dteCombobox2 = new DataTemplate();

Style styleComboBox2 = new Style(typeof(ComboBox));
Setter setterStyleComboBox2 = new Setter();
setterStyleComboBox2.Property = ComboBox.VisibilityProperty;
setterStyleComboBox2.Value = Visibility.Visible;
styleComboBox2.Setters.Add(setterStyleComboBox2);

DataTrigger datatriggerStyleComboBox2 = new DataTrigger();
datatriggerStyleComboBox2.Value = "Hello";
Binding bindingDatatriggerStyleComboBox2 = new Binding("{Binding ControlType}");
datatriggerStyleComboBox2.Binding = bindingDatatriggerStyleComboBox2;

Setter setterDatatriggerStyleComboBox2 = new Setter();
setterDatatriggerStyleComboBox2.Property = ComboBox.VisibilityProperty;
setterDatatriggerStyleComboBox2.Value = Visibility.Hidden;
datatriggerStyleComboBox2.Setters.Add(setterDatatriggerStyleComboBox2);

styleComboBox2.Triggers.Add(datatriggerStyleComboBox2);

FrameworkElementFactory fefCombobox2 = new FrameworkElementFactory(typeof(ComboBox));
fefCombobox2.SetValue(ComboBox.WidthProperty, 140D);
fefCombobox2.SetValue(ComboBox.ItemsSourceProperty, dtTest.DefaultView);
fefCombobox2.SetValue(ComboBox.StyleProperty, styleComboBox2);

dteCombobox2.VisualTree = fefCombobox2;
gvcCombobox2.CellTemplate = dteCombobox2;
gvCursisten.Columns.Add(gvcCombobox2);

最佳答案

尝试这样的事情。您可以遍历 gridview 的每一行并绑定(bind)数据或使用 FindControl 设置 gridview 中控件的可见性。

protected void gvCursisten_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
ComboBox cb = (e.Row.FindControl("gvcomboboxid") as ComboBox);
if(some_value == null)
{
cb.Visible = false;
}
else
{
cb.Visible = true;
}
}
}

关于c# - 如何更改 gridview 中组合框的可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54323273/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com