Hỏi đáp

Chia sẻ kiến thức, cùng nhau phát triển

Set StackPanel Collapsed

3 năm trước 579 lượt xem 6 bình luận 3 năm trước

Xin chào mọi người, Minh mới tập lập trình. Mình có gặp khó khăn như sau

+ Mình tạo 1 toolbar dùng chung , có 1 StackPanel   chứa các button

+ Mình muốn khi mở 1 cửa sổ khác và có thể set Visible cho StackPanel  thành Collapsed

+ Mình google rất nhiều nhưng đến giờ vẫn không làm được. Mong mọi người chỉ giúp.

Cảm ơn mọi người rất nhiều.

 

Bình luận

Để bình luận, bạn cần đăng nhập bằng tài khoản Howkteam.

Đăng nhập
Supporter Moderator đã bình luận 3 năm trước

Bạn sửa lại code như bên dưới nha
 

UCTopTitlebar.cs


using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace MyKeep.UC
{
    /// <summary>
    /// Interaction logic for CtrTopTitlebar.xaml
    /// + Behind Ctr dùng xử lý các Style tác vụ cục bộ của Ctrol
    /// + Class Ctr..Action dùng xử lý các sự kiện theo yêu từ các Form khác yêu cầu
    /// </summary>
    public partial class UCTopTitlebar : UserControl
    {
        public UCTopTitlebar()
        {
            InitializeComponent();
            DataContext = new UCTopTitlebarModel();// gán context tới Class xử lý Click
        }

        private void BTExit_Click(object sender, RoutedEventArgs e)
        {
            Window.GetWindow(this).Close();
        }

        private void BTMini_Click(object sender, RoutedEventArgs e)
        {
            Window.GetWindow(this).WindowState = WindowState.Minimized;
        }

        private void TitleBar_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
            {
                Window.GetWindow(this).DragMove();
            }
        }


        private static readonly DependencyProperty IsShowButtonAccProperty = DependencyProperty.Register("IsShowButtonAcc", typeof(bool), typeof(UCTopTitlebar), new PropertyMetadata(true));
        public bool IsShowButtonAcc
        {
            get { return (bool)GetValue(IsShowButtonAccProperty); }
            set { SetValue(IsShowButtonAccProperty, value); }
        }


        private static readonly DependencyProperty IsShowButtonSettingProperty = DependencyProperty.Register("IsShowButtonSetting", typeof(bool), typeof(UCTopTitlebar), new PropertyMetadata(true));
        public bool IsShowButtonSetting
        {
            get { return (bool)GetValue(IsShowButtonSettingProperty); }
            set { SetValue(IsShowButtonSettingProperty, value); }
        }


        private static readonly DependencyProperty IsShowToggleButtonProperty = DependencyProperty.Register("IsShowToggleButton", typeof(bool), typeof(UCTopTitlebar), new PropertyMetadata(true));
        public bool IsShowToggleButton
        {
            get { return (bool)GetValue(IsShowToggleButtonProperty); }
            set { SetValue(IsShowToggleButtonProperty, value); }
        }

    }   
}

UCTopTitlebar.xaml

<UserControl x:Class="MyKeep.UC.UCTopTitlebar"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
             xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"   
             
             xmlns:local="clr-namespace:MyKeep.UC" 
             
             d:DataContext="{d:DesignInstance Type=local:UCTopTitlebarModel }" 
             mc:Ignorable="d"    Name="isCtrTopTitlebar" Tag="title_bar"
             >
  


    <Grid> 
        <Grid.Resources> 
            <Style x:Key="sBtBasic" TargetType="Button" BasedOn="{StaticResource MaterialDesignFloatingActionAccentButton}">
                <Setter Property="Width" Value="25" />
                <Setter Property="Height" Value="25" /> 
                <Setter Property="Cursor" Value="Hand" />

                <Setter Property="Foreground" Value="White" />
                <Setter Property="Background" Value="{x:Null}" />
                <Setter Property="BorderBrush" Value="{x:Null}" />
            </Style>

            <Style x:Key="sBtRight"  TargetType="Button" BasedOn="{StaticResource sBtBasic}"> 
                <Setter Property="DockPanel.Dock" Value="Right" />
                <Setter Property="Margin" Value="0 0 5 0" />
            </Style>

            <Style x:Key="sBtLeft"  TargetType="Button" BasedOn="{StaticResource sBtBasic}">
                <Setter Property="DockPanel.Dock" Value="Left" />
                <Setter Property="Margin" Value="5 0 0 0" />
            </Style> 
        </Grid.Resources>
 
        <DockPanel  x:Name="TitleBar" Height="45" Mouse.MouseDown="TitleBar_MouseDown"
                    LastChildFill="False" Background="#FF747373" 
                    HorizontalAlignment="Stretch" VerticalAlignment="Top"   > 
            
            
            <!-- Right Button -->
            <Button x:Name="BTExit" Style="{StaticResource sBtRight}" Click="BTExit_Click"  >
                <materialDesign:PackIcon Kind="Power"/>
            </Button>

            <Button x:Name="BTWmini" Style="{StaticResource sBtRight}" Click="BTMini_Click"  >
                <materialDesign:PackIcon Kind="WindowMinimize"/>
            </Button>

            <Button x:Name="BTPin" Style="{StaticResource sBtRight}" >
                <materialDesign:PackIcon Kind="Pin" /> 
            </Button>


            <!--  Left Button -->
            <StackPanel Orientation="Horizontal" 
                        x:Name="StackBTLeft"    > 
                
                <!-- === Collapsed Stack === :(( -->
                <!--<StackPanel.Triggers>
                    <DataTrigger  Binding="{Binding ElementName=StackBTLeft, Path=Tag}" Value="hiden">
                        <Setter Property="Visibility" Value="Collapsed"/>
                    </DataTrigger>
                </StackPanel.Triggers>-->

                <Button x:Name="BTAcc" Height="32" Width="32" Margin="10 0 0 0" Visibility="{Binding IsShowButtonAcc,RelativeSource={RelativeSource AncestorType=UserControl},Converter={StaticResource BooleanToVisibilityConverter}}"
                    Style="{StaticResource sBtBasic}" 
                    Command="{Binding CmdLoginAlert}" 
                    CommandParameter="{Binding ElementName=isCtrTopTitlebar}">
                    <materialDesign:PackIcon Kind="AccountCircle"  Width="30" Height="30" />
                </Button>


                <Button x:Name="BTOpt" Style="{StaticResource sBtLeft}" Visibility="{Binding IsShowButtonSetting,RelativeSource={RelativeSource AncestorType=UserControl},Converter={StaticResource BooleanToVisibilityConverter}}">
                    <materialDesign:PackIcon Kind="Settings"/>
                </Button>

                <ToggleButton x:Name="BTTheme"  Height="15" Cursor="Hand"  Margin="5 0 0 0"  Visibility="{Binding IsShowToggleButton,RelativeSource={RelativeSource AncestorType=UserControl},Converter={StaticResource BooleanToVisibilityConverter}}"  
                              Style="{StaticResource MaterialDesignSwitchAccentToggleButton}" IsChecked="False" /> 
            </StackPanel>  

            <TextBlock  x:Name="BTTxt" FontSize="20"
                        VerticalAlignment="Center"  Margin="20 0 20 3" Foreground="White"  
                        Text="{Binding Tag, ElementName=isCtrTopTitlebar}"></TextBlock>
                        <!--ElementName: nó như là Context để bik truyền cho ai--> 
        </DockPanel>

    </Grid>
</UserControl>

Nơi dùng UCTopTitlebar

<uc:UCTopTitlebar Grid.Row="0" Tag="{Binding Title, ElementName=isMainWin}" 
                              IsShowButtonAcc="True"
                              IsShowButtonSetting="False"
                              IsShowToggleButton="False"/>

Mình dùng DenpendencyProperty để tạo thêm thuộc tính cho UCTopTitlebar sau đó mình Binding các thuộc tính mình mới tạo với thuộc tính Visibility 

ttuanquoc đã bình luận 3 năm trước

Cảm ơn Bạn rất nhiều.

Mình làm theo các bài hướng dẫn của Kteam trên Youtube và làm mấy cái này. và mình cũng thử dùng <StackPanel.Triggers> <DataTrigger> nhưng không biết làm sao " truyền tham số cho Toolbar " và sử lý lam sao. mình google 2 ngày mà ko tìm được cách làm.

+ UCTopTitlebar trong Window Main xài thì để hiển thị đầy đủ button

+ UCLogin xài chung UCTopTitlebar và cần ẩn các button bớt

Supporter Moderator đã bình luận 3 năm trước

Bạn bổ sung phần Code này lên mình check thử, hoặc bạn có thể truyền tham số cho Toolbar để ẩn hiện tùy ý

Câu hỏi mới nhất