亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何向用戶控件中的復選框添加功能

如何向用戶控件中的復選框添加功能

C#
臨摹微笑 2022-12-24 14:51:12
我有一個用戶控件,它是一個顯示復選框的下拉菜單。有一個調用 SetText 函數的復選框單擊事件,該函數根據已選擇的內容(我想保留)設置文本。我還想通過設置自定義函數的命令向用戶控件添加一個函數。例如,當他們選中一個復選框時,我可以調用視圖模型中設置的函數,并保留 SetText 函數。我嘗試向復選框添加一個命令。以及命令的 usecontrol 的依賴屬性。另外還有一個在視圖模型中使用的簡單函數-UserControl.xaml <ComboBox    x:Name="CheckableCombo"    SnapsToDevicePixels="True"    OverridesDefaultStyle="True"    ScrollViewer.HorizontalScrollBarVisibility="Auto"    ScrollViewer.VerticalScrollBarVisibility="Auto"    ScrollViewer.CanContentScroll="True"    IsSynchronizedWithCurrentItem="True"    MinWidth="120"    MinHeight="20"    ItemsSource="{Binding ElementName=UserControl, Path=ItemsSource}"    DataContext="{Binding ElementName=UserControl, Path=DataContext}"    >    <ComboBox.ItemTemplate>        <HierarchicalDataTemplate>            <CheckBox Content="{Binding Title}"                      IsChecked="{Binding Path=IsSelected, Mode=TwoWay}"                      Tag="{RelativeSource FindAncestor, AncestorType={x:Type ComboBox}}"                      Click="CheckBox_Click"                      Command="{Binding YourCommand}"              />   <i:Interaction.Triggers>       <i:EventTrigger EventName="SelectionChanged">         <i:InvokeCommandAction Command="{Binding YourCommand}" />      </i:EventTrigger>     </i:Interaction.Triggers>-UserControl.xaml.cs    public ICommand YourCommand    {        get { return (ICommand)GetValue(YourCommandProperty); }        set { SetValue(YourCommandProperty, value); }    }   
查看完整描述

1 回答

?
慕容3067478

TA貢獻1773條經驗 獲得超3個贊

我簡化了您的用戶控件,使其更具可讀性,并且只關注工作的外部命令。我修改命令的綁定。在列表中,您獲得了項目的本地數據上下文,但需要將命令綁定到外部數據上下文。


 <ComboBox ItemsSource="{Binding ElementName=UserControl, Path=ItemsSource}">

      <ComboBox.ItemTemplate>

          <HierarchicalDataTemplate>

               <CheckBox Content="{Binding .}"

                   Click="CheckBox_Click"

                   Command="{Binding ElementName=UserControl,Path=YourCommand}"> 

                </CheckBox>

          </HierarchicalDataTemplate>

       </ComboBox.ItemTemplate>

 </ComboBox>

在 UserControl1.cs 中我得到:


public ICommand YourCommand

{

    get { return (ICommand)GetValue(YourCommandProperty); }

    set { SetValue(YourCommandProperty, value); }

}


// Using a DependencyProperty as the backing store for YourCommand.  This enables animation, styling, binding, etc...

public static readonly DependencyProperty YourCommandProperty =

            DependencyProperty.Register("YourCommand", typeof(ICommand), typeof(UserControl1), new PropertyMetadata(null));

我測試過,它對我有用。


查看完整回答
反對 回復 2022-12-24
  • 1 回答
  • 0 關注
  • 91 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號