我有一个包含 ItemTemplate 和 StackPanel 的列表框。我将一个通用列表分配给我的列表框项目源。我想更改该堆栈面板的可见性。 (当我单击鼠标左键“closeAll”并关闭列表框中的所有 stackPanel 项目时,将它的可见性更改为折叠)。我想用 stackpanel visibility={Binding Acikmi} 来做到这一点。 我有 PastAndOr 类及其属性“Acikmi”,它的第一个值是“可见”。 如何使用绑定(bind)双向模式将“Acikmi”值从“可见”更改为“折叠”?
public static List<PastAndOr> GetPastOr()
{
string connStr = "server=localhost;user=root;database=carbovisor;port=3306;password=12345";
MySqlConnection conn = new MySqlConnection(connStr);
conn.Open();
MySqlCommand cmd = new MySqlCommand("SELECT s.sarjno as sarjno,s.createdate as createdate,m.adi as madi,mp.parcakodu as parcakodu,mp.parcaadi as parcaadi,mp.malzeme as malzeme,mr.kodu as musterikodu,mr.adi as musteriadi FROM t_sepetler s JOIN t_mamul m ON m.adi=@receteadi OR m.id=s.mamulid LEFT JOIN t_musteriparca mp ON mp.firmaid=s.musteriid LEFT JOIN t_musteriler mr ON mr.id=mp.firmaid WHERE s.createdate >= @tarih1 AND s.createdate <= @tarih2 GROUP BY sarjno,madi ORDER BY createdate DESC", conn);
cmd.Parameters.AddWithValue("@tarih1", PastProcess.tarih1);
cmd.Parameters.AddWithValue("@tarih2", PastProcess.tarih2);
cmd.Parameters.AddWithValue("@receteadi", PastProcess.receteadi);
cmd.Parameters.AddWithValue("@sarjno", PastProcess.sarjno);
MySqlDataReader dataReader = cmd.ExecuteReader();
List<PastAndOr> or = new List<PastAndOr>();
PastAndOr kayit2;
int IDSayac = 1;
while (dataReader.Read())
{
kayit2 = new PastAndOr();
kayit2.Adi = dataReader["madi"].ToString();
kayit2.SarjNo = dataReader["sarjno"].ToString();
kayit2.CreateDate = dataReader["createdate"].ToString();
kayit2.ParcaKodu = dataReader["parcakodu"].ToString();
kayit2.ParcaKoduAdi = dataReader["parcakodu"].ToString() + " - " + dataReader["parcaadi"].ToString();
kayit2.Malzeme = dataReader["malzeme"].ToString();
kayit2.MusteriKodu = dataReader["musterikodu"].ToString();
kayit2.MusteriKoduAdi = dataReader["musterikodu"].ToString() + " - " + dataReader["musteriadi"].ToString();
kayit2.Acikmi = Visibility.Visible;
kayit2.ID = IDSayac;
or.Add(kayit2);
IDSayac++;
}
conn.Close();
return or;
}
private void closeAll_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
// what goes here?
}
public class PastAndOr
{
public Visibility Acikmi { get; set; }
public int ID { get; set; }
public string Adi { get; set; }
public string SarjNo { get; set; }
public string CreateDate { get; set; }
public string ParcaKodu { get; set; }
public string ParcaKoduAdi { get; set; }
public string Malzeme { get; set; }
public string MusteriKodu { get; set; }
public string MusteriKoduAdi { get; set; }
}
XAML 代码
<ListBox x:Name="listBoxEditPast" SelectionMode="Single" Margin="0" Background="#272B34" ScrollViewer.VerticalScrollBarVisibility="Visible">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Border Grid.Row="0" BorderThickness="4,0,0,0" Margin="2,0,0,0" Height="29" Background="#2E323B" Width="1050" BorderBrush="#1373A9" MouseLeftButtonDown="Border_MouseLeftButtonDown">
<DockPanel Name="dockPanelPast" Margin="0,4,0,0">
<Image Name="imgArrow" Source="images/down-arrow.png" HorizontalAlignment="Left" Width="20" Height="18"/>
<TextBlock Text="{Binding CreateDate}" Name="txtTarih" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="16"/>
<TextBlock Text="{Binding SarjNo}" Name="txtSarjNo" Foreground="#FF9CA518" HorizontalAlignment="Stretch" VerticalAlignment="Center" FontSize="16" Margin="50,0,0,0" Width="90"/>
<TextBlock Text="{Binding Adi}" Name="txtReceteAdi" Foreground="#FF26A053" VerticalAlignment="Center" FontSize="16" Margin="40,0,0,0" HorizontalAlignment="Stretch"/>
<Button Content="Detaylar" Style="{StaticResource BlueButton}" HorizontalAlignment="Right" VerticalAlignment="Center" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" DockPanel.Dock="Right"/>
</DockPanel>
</Border>
<StackPanel Grid.Row="1" Name="stackPanelDetay" Tag="{Binding ID}" Visibility="{Binding Acikmi,Mode=TwoWay}">
<DockPanel>
<TextBlock Text="Sipariş No" Foreground="#D9480F" VerticalAlignment="Center" />
<TextBlock Text="Parça" Foreground="#AF0FD9" VerticalAlignment="Center" Margin="50,0,0,0" Width="200" />
<TextBlock Text="Malzeme" Foreground="White" VerticalAlignment="Center" Margin="150,0,0,0" Width="90"/>
<TextBlock Text="Müşteri" Foreground="#AF0FD9" VerticalAlignment="Center" Margin="70,0,0,0" />
</DockPanel>
<DockPanel>
<TextBlock Text="{Binding ID}" Foreground="White" VerticalAlignment="Center" Width="100"/>
<TextBlock Text="{Binding ParcaKoduAdi}" Foreground="White" VerticalAlignment="Center" Margin="5,0,0,0" Width="200" />
<TextBlock Text="{Binding Malzeme}" Foreground="White" VerticalAlignment="Center" Margin="152,0,0,0" Width="90" />
<TextBlock Text="{Binding MusteriKoduAdi}" Foreground="White" VerticalAlignment="Center" Margin="70,0,0,0" />
</DockPanel>
</StackPanel>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
最佳答案
你的类应该实现 INoyifyPropertyChanged 接口(interface)。阅读有关 WPF INoyifyPropertyChanged 的更多详细信息。
public class PastAndOr:INotifyPropertyChanged
{
public Visibility _acikmi;
public Visibility Acikmi
{
get {return _acikmi; }
set
{
_acikmi = value;
OnPropertyChaged(nameof(Acikmi));
}
}
public int ID { get; set; }
public string Adi { get; set; }
public string SarjNo { get; set; }
public string CreateDate { get; set; }
public string ParcaKodu { get; set; }
public string ParcaKoduAdi { get; set; }
public string Malzeme { get; set; }
public string MusteriKodu { get; set; }
public string MusteriKoduAdi { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChaged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
XAML 就像
<StackPanel Grid.Row="1" Name="stackPanelDetay" Tag="{Binding ID}" Visibility="{Binding Acikmi}">
关于c# - 如何在列表框中使用双向绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45418987/