2014-02-08 30 views
1

嗨我正在嘗試附加一個函數到文本框用於輸入一些輸入信息從資源字典中加載到接口。這裏是XML,控件事件沒有從鏈接到資源字典的類觸發wpf

  <ContentControl> 
      <Grid> 
       <Image s:DesignerItem.Code ="1773" IsHitTestVisible="False" Stretch="Fill" Source="../Images/addition.png" ToolTip="Addition" /> 
       <TextBox Height="57" Width="56" Margin="13,13,130,13" BorderThickness="0" FontSize="45" HorizontalAlignment="Center" VerticalAlignment="Center" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" TextChanged="msg_TextChange" KeyUp="msg_MouseDown"/> 
       <TextBox Height="57" Width="56" Margin="132,13,12,13" BorderThickness="0" FontSize="45" HorizontalAlignment="Center" VerticalAlignment="Center" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" KeyDown="msg_MouseDown"/> 
       <Button MouseDown="msg" Width="50" Height="20">ck</Button> 
      </Grid> 
      </ContentControl> 

從上面的代碼,我試圖使用幾種不同類型的控制事件。我成功地鏈接了我的函數將要放入的類,使用以下代碼將類鏈接到資源字典。

x:Class="DiagramDesigner.CodeBuilding" 
x:ClassModifier="public" 

下面是我使用的類的代碼,

public partial class CodeBuilding : ResourceDictionary 
{ 
    public CodeBuilding() 
    { 
     InitializeComponent(); 
    } 

    public void msg_TextChange(object sender,EventArgs e) 
    { 
     MessageBox.Show("oh no, you've got me ..."); 
    } 

    public void msg(object sender, MouseEventArgs e) 
    { 
     MessageBox.Show("oh no, you've got me ..."); 
    } 
} 

正如你所看到的,我只是用一個簡單的消息,表示如果該事件已經被解僱,項目成功生成並且運行良好,但是當我嘗試觸發XML中使用的任何事件時,綁定到事件的函數根本不會觸發。

我不確定如果這是將函數鏈接到由資源字典加載的事件的最佳方法,但任何人都可以爲我遇到的這個問題提供一些指導。

回答

0

XAML文件應該依賴於文件後面的部分代碼以使事件生效。

確保構建文件後面的代碼的操作設置爲Compile

還可以在記事本或任何文本編輯器中打開.csproj文件,並確保在XAML文件中設置DepedentUpon屬性。它看起來是這樣的:

<Compile Include="CodeBuilding.xaml.cs"> 
    <DependentUpon>CodeBuilding.xaml</DependentUpon> 
</Compile> 

在一個側面說明,簡單的步驟,使其工作就像是:

  1. 在項目中添加一個空白UserControl。它會自動爲你做這個工作,我上面提到過。
  2. 您只需將XAML文件更改爲ResourceDictionary即可。 (將UserControl替換爲ResourceDictionary)。
  3. 而在後面的代碼中,只需將基類從UserControl更改爲ResourceDictionary即可。
+0

嗨Rohit,我試過你提出的這個解決方案,但是這個函數沒有被調用。你有什麼其他的建議? – Daniel

+0

你有沒有試過第二個建議? –

+0

是的,我做到了。我添加了第二個解決方案作爲我正在工作的項目的一個附加組件,它仍然沒有激發任何消息窗口:(。 – Daniel