iT邦幫忙

0

WPF 可編輯文字的radiobutton

  • 分享至 

  • xImage

以前都在使用winform,現在換成WPF遇到些問題,還請各位指教,謝謝

目前使用WPF設計了一個radiobutton自定義控件,其中包含一個textblock和textbox
當這個radiobutton被點兩下就把textblock隱藏並把textbox顯示,同時textbox內容會和textblock一樣

<RadioButton
    x:Name="Radiobutton1"
    Background="{x:Null}"
    Margin="5"
    >
    <StackPanel>
        <StackPanel x:Name="StackPanel1" Orientation="Horizontal">
            <TextBlock
                x:Name="NameTextBlock1"
                Width="80"
                Margin="4,2,4,0"
                VerticalAlignment="Center"
                FontSize="10"
                MouseLeftButtonDown="TextBlock_MouseLeftButtonDown"
                Background="{x:Null}"
                Text="{Binding Text, RelativeSource={RelativeSource AncestorType={x:Type local:EditRadiobutton}}}">
            </TextBlock>
            <TextBox
                x:Name="NameTextBox1"
                Width="80"
                Margin="0,0,0,0"
                HorizontalAlignment="Left"
                KeyDown="PortNameTextBox_KeyDown"
                LostFocus="PortNameTextBox_LostFocus"
                MaxLength="6"
                MaxLines="1"
                Background="{x:Null}"
                Visibility="Collapsed" />
        </StackPanel>
    </StackPanel>
</RadioButton>

這是雙擊文字方塊後的事件,SetControl是抓textBox使用的,不確定是不是這部分有衝突

private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            SetControl(sender);
            
            e.Handled = true;
            textBox.Text = textBlock.Text;
            textBox.Visibility = Visibility.Visible;
            textBlock.Visibility = Visibility.Collapsed;
            textBox.Focus();
            
        }

但我在使用這個控件的頁面C#部分,使用以下程式碼指派事件,卻無法執行RadioButton_Checked,但雙擊修改文字的功能還在

customRadio.Checked += RadioButton_Checked;

想請問應該怎麼修改才能實現單擊執行RadioButton_Checked事件,雙擊執行修改內容事件,謝謝各位

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
kw6732
iT邦研究生 5 級 ‧ 2023-10-23 11:32:07
最佳解答

這我有遇過,你這裡的語法看起來是code behind的寫法,目前我只能猜不確定該程式碼會不會執行到,你可以試著把它寫在xaml內,看能否解決你的問題,xaml是會在載入時由InitializeComponent()執行,可確定一定會被執行到。
針對不同的Event,我有用手頭的專案測試過了,但這會有莫名的干擾問題,建議改如下語法。

示範如下:

<RadioButton 
x:Name="CombinOption" 
GroupName="DataShow" 
IsChecked="True" 
MouseRightButtonDown="Radio_MouseLeftButtonDown"
MouseDoubleClick="Radio_MouseDoubleClick" />

如果要採用Code behind 請使用匿名函式加入,作法如下(請同時加入),要閃開原有的Checked 事件,因為它包含了MouseLeftButtonDown等幾個事件,會有干擾,不然就換個物件使用。


// 用滑鼠右鍵
CombinOption.MouseRightButtonDown += (Object sender, MouseButtonEventArgs e) =>
{
    MessageBox.Show(e.OriginalSource.ToString(), "Right");
};

// 用滑鼠雙擊
CombinOption.MouseDoubleClick += (Object sender, MouseButtonEventArgs e) =>
{
    MessageBox.Show(e.OriginalSource.ToString(), "Double");
};
看更多先前的回應...收起先前的回應...

感謝回覆
因為我是以code behind的方式,把該元件加入到view中,所以沒有寫在xaml中
另外我有新增一個頁面做實驗,使用Checked=這種方式直接指定也無法

kw6732 iT邦研究生 5 級 ‧ 2023-10-23 13:15:17 檢舉

已經用編輯內文回應你了~

非常感謝,測試了一下可以跳出MessageBox了,我在改成我要的試試看,如果有問題還方便幫忙解答嗎?

剛才測試右鍵、滑鼠雙擊都可以正常執行,所以換成左鍵MouseLeftButtonDown嘗試,卻沒有辦法辦法跳出messagebox

editRadio.MouseRightButtonDown += (Object sender, MouseButtonEventArgs e) =>
{
    MessageBox.Show(e.OriginalSource.ToString(), "Right");
};
editRadio.MouseDoubleClick += (Object sender, MouseButtonEventArgs e) =>
{
    MessageBox.Show(e.OriginalSource.ToString(), "Double");
};
editRadio.MouseLeftButtonDown += (Object sender, MouseButtonEventArgs e) => {
    MessageBox.Show(e.OriginalSource.ToString(), "Left");
};
kw6732 iT邦研究生 5 級 ‧ 2023-10-23 16:46:45 檢舉

我有測試出來 左鍵是不行的

如果要採用Code behind 請使用匿名函式加入,作法如下(請同時加入),要閃開原有的Checked 事件,因為它包含了MouseLeftButtonDown等幾個事件,會有干擾,不然就換個物件使用。

MouseLeftButtonDown Checked 這兩個 Event 會和 DoubleClick相互干擾

我要發表回答

立即登入回答