2012-02-15 21 views
13

標題爲 我在SO中看到過幾個類似的問題thisthis,但我沒有看到它的解決方案。是否可以在不設置DataContext的情況下綁定代碼隱藏屬性?

我知道,如果我需要綁定到代碼beind,我需要設置Datacontext = this

但我的問題是,我的DataContext已經綁定到我的視圖模型,但我想做一些UI操作使用命令這是在代碼中定義的。

是否可以將其綁定到xaml中?如果是這樣,怎麼樣?

編輯:我沒試過如下:

<Window x:Class="WpfApplication3.Window1" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="Window1" Height="300" Width="300" x:Name="_Root"> 
<Grid x:Name="hellogrid"> 
    <TextBlock x:Name="myTextBlock" Text="AAAA"/> 
    <Button Margin="82,119,121,120" Name="button2" Content="{Binding Path=Text, ElementName=myTextBlock}"/> 
    <Button Margin="82,72,121,0" Name="button3" Content="{Binding Path=MyText, ElementName=_Root}" Height="23" VerticalAlignment="Top" /> 
</Grid> 

和代碼隱藏:

public partial class Window1 : Window 
{ 
    public string MyText { get; set; } 

    public Window1() 
    { 
     InitializeComponent(); 
     MyText = "ABC"; 
    } 
} 

我可以看到Button2的顯示AAAA,但將Button3顯示什麼?

+0

請參閱我更新的答案,我只是測試你綁定你BUTTON3 Content屬性正常工作的方法,你只需要實現變更通知,如果你需要更改MYTEXT財產在運行時...... – 2012-02-15 18:40:07

回答

14

編輯

最好的解決辦法是IMO在此SO question的一個posted通過@Saad伊姆蘭...

有了這個解決方案,你所要做的就是命名你的窗口並綁定到你的XAML中的一個屬性,就像這樣簡單{Binding ElementName=MyWindowName, Path=MyText}

因此,你在做什麼與Content="{Binding Path=MyText, ElementName=_Root}"是完全正確的,你的Button Content屬性是綁定到MyText屬性,但你唯一缺少的是改變通知(需要實現INotifyPropertyChanged接口),所以當你設置你的MyText農行MyText = "ABC";沒有任何變化通知發送屬性...

簡單的方法來測試,這是顯式設置MYTEXT財產如:

private string myText = "ABC"; 
public string MyText 
{ 
    get { return myText; } 
    set { myText = value; } 
} 

InitializeComponent()前的構造函數設置它被稱爲:

MyText = "ABC"; 
InitializeComponent(); 

如果你這樣做,你會發現,你的按鈕將有ABC作爲它的內容,但修改MYTEXT屬性,因爲沒有任何改變的通知不會影響按鈕的內容...

+0

啊,我明白了,這就是我失蹤的原因。它現在工作完美。非常感謝! – 2012-02-15 18:40:48

5

當然,你可以使用ElementName

<Window Name="root" 
     Class="..." 
     ...> 

    ... 

    <TextBox Text="{Binding Path=Foo, ElementName=root}" /> 

你也可以用RelativeSource做到這一點,但語法是醜陋...

+0

奇怪的是,我嘗試了'ElementName'和'Binding Path = Foo,RelativeSource = {RelativeSource Self}',但兩者都不起作用。我正在使用WPF 3.5,這有什麼關係嗎? – 2012-02-15 18:13:45

+2

@KingChan,這是因爲你在調用'InitializeComponent'後設置了MyText屬性*,所以在你修改它之前已經檢索到了這個值。由於你的類沒有實現'INotifyPropertyChanged',並且你的屬性不是一個依賴屬性,所以綁定引擎無法知道這個值已經改變,所以它不會刷新按鈕的內容。 – 2012-02-15 18:51:42

9

當然

綁定有很多種類型。最基本的一個結合到所述DataContext一個屬性,它通常是從父對象繼承

<DataTemplate DataType="{x:Type MyModel}"> 
    <!-- DataContext is object of type MyModel --> 
    <local:MyView /> 
</DataTemplate> 

或者

<Window x:Name="MyWindow"> 
    <!-- DataContext Inherited from Window --> 
    <TextBlock Text="{Binding SomeProperty}" /> 
</Window> 

其中

var SomeObject = new SomeModel(); 
SomeObject.SomeProperty = "Test"; 
myWindow.DataContext = SomeObject; 

其它結合類型包括ElementName,其中您可以指定目標UI元素作爲綁定的數據源使用

<StackPanel> 
    <CheckBox x:Name="SomeCheckBox" /> 
    <TextBlock Text="{Binding ElementName=SomeCheckBox, Path=IsChecked}" /> 
</StackPanel> 

<local:MyUserControl x:Name="SomeUserControl"> 
    <Button Command="{Binding ElementName=SomeUserControl, Path=DataContext.SaveCommand}" /> 
</local:MyUserControl > 

或者RelativeSource,它可以讓你找到對象相對於當前對象作爲數據源使用

<Window Title="Test"> 
    <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=Title}" /> 
</Window> 

<local:MyUserControl> 
    <Button Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:MyUserControl}}, Path=DataContext.SaveCommand}" /> 
</local:MyUserControl > 

而且TemplateBinding ,該綁定是到的快捷方式10綁定結合到一個模板對象

<Button Content="Test"> 
    <Button.Template> 
     <ControlTemplate TargetType="{x:Type Button}"> 
      <TextBlock Text="{TemplateBinding Content}" /> 
     </ControlTemplate> 
    </Button.Template> 
</Button> 
+0

感謝您的參考!我也使用你的RelativeSource。但奇怪的是,當我使用'RelativeSource AncestorType = {x:Type Window}'時,我怎麼不需要INotifyPropertyChanged? – 2012-02-15 18:41:18

+1

這是WPF綁定的一般概述,而不是他的問題的答案...... – 2012-02-15 18:45:49

+0

@DeanKuga我不清楚在添加代碼之前最初的實際問題是什麼,並且將其解讀爲「我可以綁定到什麼而無需在DataContext中指定「。這似乎是一個廣泛的問題,所以我寫了一個廣泛的答案:)一旦代碼被添加,我可以清楚地看到OP想要完成什麼,但到那時已經發布了一個包含他所需信息的答案。 – Rachel 2012-02-15 18:59:06

相關問題