2012-07-19 76 views
0

我是新來WPF和數據綁定,所以我可能很容易錯過我的研究中的東西,或者我一直在使用錯誤的搜索條件(更可能)找到解決方案。WPF MultiValueConverter數據綁定到字符串值不參考

綁定的值似乎越來越被傳遞,而不是對象的引用,所以當值在後面的代碼中設置時不會被更新。

試圖概括一個OpenFileDialog在選項卡控件的某些不同選項卡上有用。我創建保存的參數自定義數據對象(路徑,過濾器,和TextBox)

class OpenFileCommandParameters 
{ 
    public string Filter { get; set; } 
    public string Path { get; set; } 
    public string TextBox { get; set; } 
} 
class OpenFileCommandParamtersConverter : IMultiValueConverter 
{ 
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     OpenFileCommandParameters parameters = new OpenFileCommandParameters(); 
     if (values[0] is string) parameters.Filter = (string)values[0]; 
     if (values[1] is string) parameters.Path = (string)values[1]; 
     if (values[2] is string) parameters.TextBox = (string)values[2]; 
     return parameters; 
    } 
    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

的XAML傳遞的信息是這樣的:

<TextBox Name="ButtonTagImportFileName" Text="{Binding Path=TagImportTabVM.TbFileName}" Height="23" HorizontalAlignment="Left" Margin="83,17,0,0" VerticalAlignment="Top" Width="221" /> 
<Button Name="TagImportOpenFile" Content="Open File" Command="{Binding Path=OpenFileCommand}" Height="23" HorizontalAlignment="Left" Margin="342,17,0,0" VerticalAlignment="Top" Width="98" > 
    <Button.CommandParameter> 
     <MultiBinding Converter="{StaticResource openFileCommandParametersConverter}"> 
      <MultiBinding.Bindings> 
       <Binding Source="XML files (*.xml)|*xml|All files (*.*)|*.*"/> 
       <Binding Path="AppPath"/> 
       <Binding Path="TagImportTabVM.TbFileName"/> 
      </MultiBinding.Bindings> 
     </MultiBinding> 
    </Button.CommandParameter> 

兩個文本框和打開文件按鈕具有綁定到相同的字符串屬性。

的屬性,都會通過命令

private void OpenFile(object parameter) 
    { 
     var parameters = parameter as OpenFileCommandParameters; 
     FileDialog.Filter = parameters.Filter; 
     FileDialog.InitialDirectory = parameters.Path; 
     if (parameters == null) return; 
     var result = FileDialog.ShowDialog(); 
     if (result == true) 
     { 
      parameters.TextBox = FileDialog.SafeFileName; 
     } 
    } 

一旦該命令完成,我希望TbFileName的價值是一樣的,從文件對話框什麼來執行更新。不是這種情況。如從OpenFile塊結束之前的中斷點看到的那樣。

enter image description here

我很感謝你能提供給我任何幫助。

回答

0

我對這個解決方案非常感興趣,而@小綿羊幫助我用他的解決方案指引我。只是想完全結束這個問題。

我在想,因爲源代碼沒有得到更新,當我通過代碼設置值時,我打破了綁定。不是這種情況。它只是根本不知道價值已經改變,所以它不知道更新來源。

一旦我發現這一點,直接瞭解如何通知系統讓它知道它需要更新源代碼。這被添加於加入讓它正確地更新private void OpenFile(object parameter)

BindingExpression binding = BindingOperations.GetBindingExpression(parameters.PassedTextBox, TextBox.TextProperty); 
binding.UpdateSource(); 

這就是所有。

0

我相信這不起作用,因爲MultiBinding永遠不會嘗試更新其源綁定。它可能會迫使一些這方面的知識,但是當我嘗試使用

BindingOperations.GetBindingExpression(TagImportOpenFile, Button.CommandParameterProperty) 

它總是返回null,所以我不知道的與CommandParameters將工作綁定的方法。獲取CommandProperty的表達式工作正常,CommandParameters必須不同......不知何故。

我想辦法做到這一點,肯定會的工作將是:

一)將您的參數類轉換成視圖模型(或至少把它當作INotifyPropertyChanged的/ DependencyObject的雙向綁定),文本框屬性可能需要重新命名爲'FileName'。

二)在這種情況下將這個類的一個實例,在你的屏幕視圖模型(「TagImportTabVM」),

c)您的XAML更改爲類似以下內容:

<TextBox Name="ButtonTagImportFileName" Text="{Binding Path=OpenFileVM.FileName}" Height="23" HorizontalAlignment="Left" Margin="83,17,0,0" VerticalAlignment="Top" Width="221" /> 
<Button Name="TagImportOpenFile" Content="Open File" Command="{Binding Path=OpenFileCommand}" CommandParameter="{Binding OpenFileVM}" 
     Height="23" HorizontalAlignment="Left" Margin="342,17,0,0" VerticalAlignment="Top" Width="98" /> 

這是假設您在創建參數的新實例時不介意在代碼中設置文件過濾器(因爲我假設您爲AppPath做了這樣的事情)。